【发布时间】:2014-08-13 20:31:32
【问题描述】:
我有一个这样的测试:
require 'rails_helper'
RSpec.describe User, :type => :model do
it { should have_many(:assignments) }
it { should have_many(:roles).through(:assignments) }
end
返回此错误:
Failure/Error: it { should have_many(:assignments) }
Expected User to have a has_many association called assignments (Assignment does not have a user_id foreign key.)
但是我的架构是这样的:
create_table "assignments", force: true do |t|
t.integer "user_id"
t.integer "role_id"
t.datetime "created_at"
t.datetime "updated_at"
end
我的模型如下所示:
user.rb
require 'role_model'
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :first_name, presence: true
validates :last_name, presence: true
validates :password, length: { minimum: 8 }
include RoleModel
roles_attribute :roles_mask
roles :admin, :super_admin, :user
has_many :assignments
has_many :roles, :through => :assignments
end
这里是assignment.rb
class Assignment < ActiveRecord::Base
belongs_to :user
belongs_to :role
end
知道我在这里做错了什么吗?
【问题讨论】:
标签: ruby-on-rails rspec shoulda