Skip to content

Instantly share code, notes, and snippets.

@khan-hasan
Created December 28, 2017 15:45
Show Gist options
  • Save khan-hasan/cb9b0862bb3dba5bf6071bb25e9d858c to your computer and use it in GitHub Desktop.
Save khan-hasan/cb9b0862bb3dba5bf6071bb25e9d858c to your computer and use it in GitHub Desktop.
require 'rails_helper'
describe Product do
context "when the product has comments" do
let(:product) { Product.create!(name: "race bike") }
let(:user) { User.create!(first_name: "John", last_name: "Smith", email: "[email protected]", password: "password123") }
before do
product.comments.create!(rating: 1, user: user, body: "Awful bike!")
product.comments.create!(rating: 3, user: user, body: "Ok bike!")
product.comments.create!(rating: 5, user: user, body: "Great bike!")
end
it "returns the average rating of all comments" do
expect((product.comments.rating(1) + product.comments.rating(2) + product.comments.rating(3))/3).to eq 3
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment