Ruby on rails rspec关联创建错误
我有一个模型Ruby on rails rspec关联创建错误,ruby-on-rails,factory-bot,rspec2,Ruby On Rails,Factory Bot,Rspec2,我有一个模型项目有很多评分和一个评分属于项目评分属于用户我想强制创建项目的用户也对其进行评分。其他用户随后可以对其进行评分。项目和用户在我的模型中没有关联。 我在我的item_spec中做了以下操作,这给了我一个错误没有将符号隐式转换为整数行@item=item.new(下面的名称:“Item1”) class Item < ActiveRecord::Base has_many :ratings, dependent: :destroy, inverse_of: :item ac
项目有很多评分
和一个评分属于项目
评分属于用户
我想强制创建项目的用户也对其进行评分。其他用户随后可以对其进行评分。项目和用户在我的模型中没有关联。
我在我的item_spec中做了以下操作,这给了我一个错误没有将符号隐式转换为整数行@item=item.new(下面的名称:“Item1”
)
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
class项true
验证:name,:length=>{minimum:3}
验证:category,:length=>{minimum:3}
验证是否存在:评级
结束
需要“spec\u helper”
描述你要做的事情
在做之前
@item=item.new(名称:“Item1”,
网址:“www.item1.com”,
完整地址:“Item1Address”,
城市:“项目1城市”,
国家:“项目1国家”,
类别:“Item1Type”,
评级属性:{“评级”=>“3”,“评论”=>“啊好”})
结束
我也在用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
工厂:项目do
在创建do之前|
r、 评级正在工作的代码,现在在测试某些关联顺序时遇到问题,但至少所需的功能正在工作
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
这是规范文件
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 }
项目的模型文件中没有任何更改您是否要共享stacktrace或确认您的规范中的行位于跟踪的顶部?此外,您是否要共享项目
模型中的初始化
定义(如果有?@PeterAlfvin添加了错误2)所需的stacktrace信息)项目失败/错误:@Item=Item.new(名称:“Item1”,类型错误:符号未隐式转换为整数#。/spec/models/Item_spec.rb:5:in
块(2级)在'`中,我假设在上面的代码中,您已将项目
模型文件与规范文件连接起来,因为否则行号#5就没有意义。我感到困惑的一点是,您有有许多:评级
,这意味着存在评级
(单数)类,但您也有一个评级
(复数)类和项目
的评级
属性。您愿意共享您的评级
和评级
模型吗?类评级{order('created_at DESC')}验证:user\u id,:presence=>true验证\u presence\u of:item验证\u numericality\u of:rating,:大于\u或等于\u to=>0验证\u numericality\u of:rating,:小于\u或等于\u to=>5 end