在工厂女工中建立协会

Building Associations in Factory Girl

我有两个模型 UsersAccounts 有关系

Users has_many Accounts via `created_by_id`

users.rb

FactoryGirl.define do
  factory :user do
    first_name {Faker::Name.first_name}
    last_name {Faker::Name.last_name}
    email {Faker::Internet.email}
    username {Faker::Internet.user_name}
    password {Faker::Internet.password}
  end

end

accounts.rb

FactoryGirl.define do
  factory :account do
    name {Faker::Company.name}
    association :user
  end

end

型号

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable,
         :validatable, :confirmable, :invitable

  has_many :accounts

  # validations
  validates :first_name, presence: true
  validates :last_name, presence: true
  validates :email, presence: true,
                    uniqueness: {case_sensitive: false},
                    :email => true
  validates :username, uniqueness: {case_sensitive: false, allow_blank: true}

  before_validation :downcase_attributes


  def name
    "#{first_name} #{last_name}"
  end

  private

  def downcase_attributes
    self.email = email.try(:downcase)
    self.username = username.try(:downcase)
  end
end

账户模型

class Account < ActiveRecord::Base

  extend FriendlyId
  friendly_id :name, use: :slugged

  belongs_to :users, foreign_key: "created_by_id"


  # Methods

  def should_generate_new_friendly_id?
    name_changed?
    end

end

当我尝试通过以下方式测试工厂的有效性时

require 'rails_helper'

describe Account, type: :model do

  context "valid Factory" do
    it "has a valid factory" do
      expect(build(:account)).to be_valid
    end
  end

  context "validations" do
    before { create(:account) }

    context "presence" do
      it { should validate_presence_of :name }
    end

  end

end

错误

Account
  valid Factory
    has a valid factory (FAILED - 1)
  validations
    presence
      example at ./spec/models/account_spec.rb:15 (FAILED - 2)

Failures:

  1) Account valid Factory has a valid factory
     Failure/Error: expect(build(:account)).to be_valid
     NoMethodError:
       undefined method `user=' for #<Account:0x007f858a4783a0>
     # ./spec/models/account_spec.rb:7:in `block (3 levels) in <top (required)>'

  2) Account validations presence
     Failure/Error: before { create(:account) }
     NoMethodError:
       undefined method `user=' for #<Account:0x007f8595154088>
     # ./spec/models/account_spec.rb:12:in `block (3 levels) in <top (required)>'

Finished in 0.87731 seconds (files took 5.61 seconds to load)
2 examples, 2 failures

Failed examples:

rspec ./spec/models/account_spec.rb:6 # Account valid Factory has a valid factory
rspec ./spec/models/account_spec.rb:15 # Account validations presence

这一行:

belongs_to :users, foreign_key: "created_by_id"

应改为:

 belongs_to :user, foreign_key: :created_by_id # notice singular :user

来自docs

A belongs_to association sets up a one-to-one connection with another model, such that each instance of the declaring model "belongs to" one instance of the other model. ...