【发布时间】: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