Last active
September 7, 2015 18:44
-
-
Save AttyC/a73409b5d0325bb2b260 to your computer and use it in GitHub Desktop.
unit test failing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
attyc@appcity:~/workspace (comments) $ rspec | |
F | |
Failures: | |
1) Product#average_rating when the product has comments returns the average rating of all comments | |
Failure/Error: @product.comments.create!(:rating => 1, :user_id => 10, :body => "hello") | |
ActiveRecord::RecordInvalid: | |
Validation failed: User can't be blank | |
# /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/validations.rb:79:in `raise_record_invalid' | |
# /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/validations.rb:43:in `save!' | |
# /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/attribute_methods/dirty.rb:29:in `save!' | |
# /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/transactions.rb:291:in `block in save!' | |
# /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/transactions.rb:347:in `block in with_transaction_returning_status' | |
# /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction' | |
# /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/transactions.rb:220:in `transaction' | |
# /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/transactions.rb:344:in `with_transaction_returning_status' | |
# /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/transactions.rb:291:in `save!' | |
# /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/associations/has_many_association.rb:39:in `insert_record' | |
# /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/associations/collection_association.rb:501:in `block (2 levels) in _create_record' | |
# /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/associations/collection_association.rb:408:in `replace_on_target' | |
# /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/associations/collection_association.rb:403:in `add_to_target' | |
# /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/associations/collection_association.rb:499:in `block in _create_record' | |
# /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/associations/collection_association.rb:183:in `block in transaction' | |
# /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction' | |
# /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/transaction.rb:188:in `within_new_transaction' | |
# /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction' | |
# /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/transactions.rb:220:in `transaction' | |
# /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/associations/collection_association.rb:182:in `transaction' | |
# /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/associations/collection_association.rb:498:in `_create_record' | |
# /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/associations/has_many_association.rb:180:in `_create_record' | |
# /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/associations/collection_association.rb:157:in `create!' | |
# /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/associations/collection_proxy.rb:306:in `create!' | |
# ./spec/models/product_spec.rb:11:in `block (4 levels) in <top (required)>' | |
Finished in 0.07311 seconds (files took 4.02 seconds to load) | |
1 example, 1 failure | |
Failed examples: | |
rspec ./spec/models/product_spec.rb:16 # Product#average_rating when the product has comments returns the average rating of all comments |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Product < ActiveRecord::Base | |
has_many :orders | |
has_many :comments | |
def average_rating | |
comments.average(:rating).to_f | |
end | |
validates :name, presence: true | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rails_helper' # also requires spec_helper and adds other stuff - if didnt need Rails just use spec_helper but unlikely | |
describe Product do | |
describe "#average_rating" do # - the '#' signifies that we are testing an instance method - describe the method you will be testing (which belongs to the Class) | |
#context 4, test 4 | |
context "when the product has comments" do # create context | |
before do # before running the test... | |
@product = Product.create!(:name => "ball") | |
@product.comments.create!(:rating => 1, :user_id => 10, :body => "hello") | |
@product.comments.create!(:rating => 3, :user_id => 11, :body => "good") | |
@product.comments.create!(:rating => 5, :user_id => 12, :body => "night") | |
end | |
it 'returns the average rating of all comments' do | |
expect(@product.average_rating).to eq 3 | |
end | |
end | |
#context 5, test 5 | |
#context "when the product has no rating" do # create context | |
#before do # before running the test... | |
# @product = Product.create(:name => "ball") | |
# @product.comments.create(:rating => '', :user_id => 10, :body => "hello") | |
#end | |
# it 'returns falsy' do | |
#expect(@product.average_rating).to be_nil | |
#end | |
#end | |
end #end describe #average_rating | |
end #end describe Product |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment