Skip to content

Instantly share code, notes, and snippets.

@AttyC
Created September 8, 2015 12:08
Show Gist options
  • Save AttyC/53fdaf5cca5d4e4b41c9 to your computer and use it in GitHub Desktop.
Save AttyC/53fdaf5cca5d4e4b41c9 to your computer and use it in GitHub Desktop.
Unite test - average_rating SUCCESS
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 1, test 1
context "when the product has comments" do # create context
before do # before running the test...
@product = Product.create(:name => "ball")
@user = User.create(:email => "[email protected]", :password => "1234567" )
@product.comments.create(:rating => 1, :user => @user, :body => "hello")
@product.comments.create(:rating => 3, :user => @user, :body => "good")
@product.comments.create(:rating => 5, :user => @user, :body => "night")
end
it 'returns the average rating of all comments' do
expect(@product.average_rating).to eq 3
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