Can I use a factory girl for fickle models

I have the following factory:

FactoryGirl.define do
  factory :poem do
    skip_create
    title "Poem title"
    intro_verse
    trait_verse
    message_verse
  end
end

for the following class of inactive recording model:

class Poem
  attr_accessor :title, :intro_verse, :trait_verse, :message_verse
end

Is it possible to create a factory for such a class?

When I run the following test:

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

I get the following error:

Failure/Error: expect(build(:poem)).to be_valid
 NoMethodError:
   undefined method `valid?'
+4
source share
2 answers

The error is that the class does not have an instance method valid?. (For Active Record models, this is defined by default)

You need to find some logic to determine if the Poem instance is valid or not, and write the method valid?accordingly.

IIRC, expect(something).to be_condition condition? something , false.

+4

ActiveModel:: Validations, , Active Record:

class Poem
    include ActiveModel::Validations

  validates :title, presence: true
  attr_accessor :title, :intro_verse, :trait_verse, :message_verse
end

poem = Poem.new
poem.valid? #false
poem.title = "title"
poem.valid? #true
+1

Source: https://habr.com/ru/post/1546686/


All Articles