我正在尝试测试“产品可以列出已对其进行过审核且不重复的用户”
这就是我的测试的样子
product_spec.rb
describe Product do
let!(:product) { Product.create }
.
.#other test
.
it "can list users that review it without duplicates" do
product.reviews.create({user_id: 1, review: "test"})
product.reviews.create({user_id: 1, review: "test2"})
product.user.uniq.count.should eq(1)
end
end
最终结果
1) Product can list users that review it without duplicates
Failure/Error: product.reviews.create({user_id: 1, review: "test"})
ActiveRecord::RecordNotSaved:
You cannot call create unless the parent is saved
# ./spec/models/product_spec.rb:49:in `block (2 levels) in <top (required)>'