需要明确factory_girl_rails的问题

时间:2015-11-25 08:34:06

标签: ruby-on-rails rspec factory-bot

我想测试模型是否有有效的工厂。当我使用create时,我得到了未定义的方法错误,但是当我使用FactoryGirl.create时它有效。为什么会这样。

以下是代码的样子:

spec/models/contact_spec.rb

require 'rails_helper'

RSpec.describe Contact, type: :model do

  it "should be a valid factory" do
    contact = create(:contact)
    expect(contact).to be_valid
  end
end

spec/factories/contact.rb

FactoryGirl.define do
  factory :contact do
    full_name     { Faker::Name.name }
    email         { Faker::Internet.email }
    phone_number  { Faker::PhoneNumber.phone_number }
    address       { Faker::Address.street_address }
  end
end

当我将规格更改为下面的内容时,我没有收到错误

require 'rails_helper'

RSpec.describe Contact, type: :model do

  it "should be a valid factory" do
    contact = create(:contact)
    expect(contact).to be_valid
  end
end

为什么会这样?

1 个答案:

答案 0 :(得分:0)

请参阅入门文档。你需要做

list

如果您没有,那么您对工厂女孩的所有电话都需要以FactoryGirl为前缀

相关问题