简体   繁体   中英

Factory_girl create gives validation error

I'm trying to test a destroy action in my rails application. I use Factory_girl to create objects. When creating a company object it's mandatory to add a user relation. But this is where I get a fail when I try to create a Company with Factory_girl.

user_spec.rb

describe User do

  before(:each) { @user = User.new(email: 'user@example.com') }

  subject { @user }

  it { should respond_to(:email) }
  it { should respond_to(:companies) }

  it "#email returns a string" do
    expect(@user.email).to match 'user@example.com'
  end

  describe "company associations" do 
    let!(:a_company) do
      FactoryGirl.create(:company, user: @user)
    end

   it {should have_many :companies, :dependent => :destroy}
  end

end

factory.rb

FactoryGirl.define do
  factory :user do
    confirmed_at Time.now
    name "Test User"
    email "test@example.com"
    password "please123"

    trait :admin do
      role 'admin'
    end
  end   
  factory :company do
    name "Total Inc."
    user :user 
  end
end

model/user.rb

class User < ActiveRecord::Base
  has_many :companies, dependent: :destroy
  enum role: [:user, :vip, :admin]
  after_initialize :set_default_role, :if => :new_record?

  def set_default_role
    self.role ||= :user
  end

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :invitable, :database_authenticatable, :registerable, :confirmable,
         :recoverable, :rememberable, :trackable, :validatable
end

model/company.rb

class Company < ActiveRecord::Base
    belongs_to :user
    validates :user_id, presence: true
end

The error I get:

Failures:

  1) User company associations
     Failure/Error: FactoryGirl.create(:company, user: @user)
     ActiveRecord::RecordInvalid:
       Validation failed: User can't be blank

EDIT

I followed the advise from below and now I can create but get following error:

Failure/Error: it {should have_many :companies, :dependent => :destroy} expected #<User:0x007fc7b7ce08c0> to respond to has_many?

The first error, Validation failed: User can't be blank , is a result of not having saved the @user object. You can fix the error by saving the object before you call create:

let!(:a_company) do
  @user.save
  FactoryGirl.create(:company, user: @user)
end

The second error, Failure/Error: it {should have_many :companies, :dependent => :destroy} expected #<User:0x007fc7b7ce08c0> to respond to has_many? is a testing error - the production code works fine. To fix your test try one of the following options:

Use Shoulda

it {should have_many(:companies).dependent(:destroy)}

Use FactoryGirl

it 'Expects user to have many companies' do
  expect{@user.companies}.to_not raise_error
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM