vendredi 7 août 2020

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

I have a simple Poro, like so:

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

A factory like so:

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

A test like so:

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

But this results in NoMethodError: undefined method 'build' for ....

Based on https://thoughtbot.com/blog/tips-for-using-factory-girl-without-an-orm, it sounds like this should work. Wondering what I am doing wrong?

Aucun commentaire:

Enregistrer un commentaire