![](/img/trans.png)
[英]Ruby on Rails trouble creating record with has_many :through relationship
[英]Having trouble with RSpec 3.1.0 and has_many through relationship with Ruby-on-Rails 4.1
我正在嘗試使用RSpec來測試模型之間的關系。 我有兩個模型:
class User < ActiveRecord::Base
has_many :user_roles
has_many :roles, through: :user_roles
end
和
class Role < ActiveRecord::Base
has_many :user_roles
has_many :users, through: :user_roles
end
通過以下類:
class UserRole < ActiveRecord::Base
belongs_to :user
belongs_to :role
end
我的RSpec代碼是:
describe User do
it { is_expected.to have_many(:user_roles) }
it { is_expected.to have_many(:roles).through(:user_roles) }
end
這兩個都引發錯誤:
1)用戶應該有很多:user_roles失敗/錯誤:它{is_expected.to have_many(:user_roles)}期望#響應has_many?
#./spec/models/user_spec.rb:49:在'
2)用戶失敗/錯誤:它{is_expected.to have_many(:roles).through(:user_roles)} NoMethodError: through' for #<RSpec::Matchers::BuiltIn::Has:0x007fe1cd003ac8> # ./spec/models/user_spec.rb:50:in
##的未定義方法through' for #<RSpec::Matchers::BuiltIn::Has:0x007fe1cd003ac8> # ./spec/models/user_spec.rb:50:in
在“( through' for #<RSpec::Matchers::BuiltIn::Has:0x007fe1cd003ac8> # ./spec/models/user_spec.rb:50:in
2級)
無論我嘗試什么,我似乎都無法使它們正常工作。 有人知道我在做什么錯嗎?
謝謝你的幫助!
我最終使用了Shoulda。 您也可以通過編程自己的匹配器來解決此問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.