[英]rspec association creation error
I have a model item has_many ratings
and a ratings belongs_to item
ratings belongs_to user
I want to force a user who is creating an item to rate it too. 我有一个模型
item has_many ratings
,一个ratings belongs_to item
ratings belongs_to user
我想强制正在创建某项的用户也对其进行评分。 Other users can then rate it later on. 然后其他用户可以稍后对其评分。 item and user have no association in my model.
项目和用户在我的模型中没有关联。 I am doing the following in my item_spec which is giving me an error
no implicit conversion of Symbol into Integer
on line @item = Item.new(name: "Item1",
below. 我正在我的item_spec中执行以下操作,这给我一个错误,
no implicit conversion of Symbol into Integer
在下面的@item = Item.new(name: "Item1",
行@item = Item.new(name: "Item1",
no implicit conversion of Symbol into Integer
。
class Item < ActiveRecord::Base
has_many :ratings, dependent: :destroy, inverse_of: :item
accepts_nested_attributes_for :ratings, :allow_destroy => true
validates :name , :length => { minimum: 3 }
validates :category , :length => { minimum: 3 }
validates_presence_of :ratings
end
require 'spec_helper'
describe Item do
before do
@item = Item.new(name: "Item1",
url: "www.item1.com",
full_address: "Item1Address",
city: "Item1City",
country: "Item1Country",
category: "Item1Type",
ratings_attributes: {"rating" => "3", "comment" => "Ahh Good"} )
end
Also using FactoryGirl I am doing something like this 也使用FactoryGirl我正在做这样的事情
factory :item do
before_create do |r|
r.ratings<< FactoryGirl.build(:ratings, item: r )
end
name "Item1"
url "www.Item1.com"
full_address "Item1Address"
city "Item1City"
country "Item1Country"
category "Item1Category"
end
factory :ratings do
rating 3
comment "Its not that bad"
user
end
end
which again is not yeilding the desired result. 再次没有达到期望的结果。
can anyone help me solve this problem please.Thanks! 谁能帮我解决这个问题。谢谢!
Working Code, now having problem testing some association order, but at least the desired functionality working. 工作代码,现在在测试某些关联顺序时遇到问题,但至少所需的功能正常工作。
factory :item do
name "Item1"
url "www.Item1.com"
full_address "Item1Address"
city "Item1City"
country "Item1Country"
category "Item1Category"
end
factory :ratings, :class => 'Ratings' do
association :item, factory: :item, strategy: :build
user
rating 3
comment "Its not that bad"
end
factory :item_with_rating, parent: :item do
ratings {[FactoryGirl.create(:ratings)]}
end
Here is the spec file 这是规格文件
require 'spec_helper'
describe Item do
before do
@item = FactoryGirl.create(:item_with_rating)
end
subject { @item }
it { should respond_to(:name) }
it { should respond_to(:url) }
it { should respond_to(:full_address)}
it { should respond_to(:city) }
it { should respond_to(:country) }
it { should respond_to(:category) }
it { should respond_to(:ratings) }
it { should_not respond_to(:type) }
it { should_not respond_to(:user_id) }
it { should be_valid }
There is no change in the Model file for item 项目的模型文件中没有更改
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.