Poro 的 FactoryBot 工厂导致未定义的方法“构建”

FactoryBot factory for a Poro results in undefined method `build'

我有一个简单的魄罗,像这样:

class Student
  attr_reader :first_name, :last_name
  def initialize(data)
    @first_name = data[:first_name]
    @last_name = data[:last_name]
  end
end

像这样的工厂:

FactoryBot.define do
  factory :student do
    first_name {"test first name"}
    last_name {"test last name"}

    # https://thoughtbot.com/blog/tips-for-using-factory-girl-without-an-orm
    initialize_with { new(attributes) }
  end
end

像这样的测试:

describe 'StudentSpec', type: :model do
  let(:student) {build(:student)}
  context 'attributes' do
    it 'respond' do
      expect(student).to respond_to(:first_name, :last_name)
    end
  end
end

但这会导致 NoMethodError: undefined method 'build' for ....

基于 https://thoughtbot.com/blog/tips-for-using-factory-girl-without-an-orm,这听起来应该可行。想知道我做错了什么?

也许您在规范文件的顶部缺少 require 'rails_helper'? 您是否也尝试添加 FactoryBot?

let(:student) { FactoryBot.build(:student) }