簡體   English   中英

通過與Ruby-on-Rails 4.1的關系遇到RSpec 3.1.0和has_many

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM