【问题标题】:Having trouble with RSpec 3.1.0 and has_many through relationship with Ruby-on-Rails 4.1通过与 Ruby-on-Rails 4.1 的关系,RSpec 3.1.0 和 has_many 遇到问题
【发布时间】:2015-02-01 19:47:55
【问题描述】:

我正在尝试使用 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:in `block (2 levels) in '

2) 用户 失败/错误:它 { is_expected.to have_many(:roles).through(:user_roles) } 无方法错误: '

中未定义的方法through' for #<RSpec::Matchers::BuiltIn::Has:0x007fe1cd003ac8> # ./spec/models/user_spec.rb:50:inblock(2 级)

无论我尝试什么,我似乎都无法让这些工作。有谁知道我做错了什么?

感谢您的帮助!

【问题讨论】:

    标签: ruby-on-rails ruby rspec has-many-through has-many


    【解决方案1】:

    我最终使用了应该。你也可以通过编写自己的匹配器来解决这个问题。

    【讨论】:

      猜你喜欢
      • 2012-06-12
      • 2010-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多