Shoulda Matcher无法找到外国身份证

时间:2014-08-13 20:31:32

标签: ruby-on-rails rspec shoulda

我有这样的测试:

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

知道我在这里做错了吗?

2 个答案:

答案 0 :(得分:3)

尝试使用更标准的&#34;参考&#34; (而不仅仅是整数):

create_table "assignments", force: true do |t|
  t.references  "user", index: true
  t.integer  "role_id"
  t.datetime "created_at"
  t.datetime "updated_at"
end

另外,您是否确保自己进行了两次迁移? 有时您需要为测试运行它们(如果您正在运行单独的rspec规范而不是使用rake运行所有这些规范)。如果有疑问:rake db:test:prepare

答案 1 :(得分:0)

我什么都没改变,一天后错误就消失了。我猜测某些东西有点不同步,但现在好了。