-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


[ ] FactoryGirl ORM

, 02 2017 . 11:03 +
FactoryGirl . , Ruby.

Rails-,
ORM ActiveRecord. JSON API Ruby .

. ; .

FactoryGirl Ruby ,
.

:


: Factory .

Factory Ruby .

it "supports building a PORO object" do
  class User
    attr_accessor :name
  end

  FactoryGirl.define do
    factory :user do
      name "Amy"
    end
  end

  user = FactoryGirl.build(:user)

  expect(user.name).to eq "Amy"
end

build . create :

NoMethodError:
  undefined method `save!' for #

: #save!.. ORM ActiveRecord
ActiveRecord::Persistence ( ) .

.


User, :

  1. attr_accessor attr_reader.
  2. JSON.

:

class User
  attr_reader :name

  def initialize(data = {})
    @name = data[:name]
  end
end

Factory :

NoMethodError:
  undefined method `name=' for #

FactoryGirl #new , . initialize_with, Factory:

t "supports custom initialization" do
  class User
    attr_reader :name

    def initialize(data)
      @name = data[:name]
    end
  end

  FactoryGirl.define do
    factory :user do
      name "Amy"

      initialize_with { new(attributes) }
    end
  end

  user = FactoryGirl.build(:user)

  expect(user.name).to eq "Amy"
end


JSON , :

{
  "name": "Bob",
  "location": {
    "city": "New York"
  }
}

Location:

class Location
  attr_reader :city

  def initialize(data)
    @city = data[:city]
  end
end

class User
  attr_reader :name, :location

  def initialize(data)
    @name = data[:name]
    @location = Location.new(data[:location])
  end
end

User Factory : Location:

it "supports constructing nested models" do
  class Location
    attr_reader :city

    def initialize(data)
      @city = data[:city]
    end
  end

  class User
    attr_reader :name, :location

    def initialize(data)
      @name = data[:name]
      @location = Location.new(data[:location])
    end
  end

  FactoryGirl.define do
    factory :location do
      city "London"

      initialize_with { new(attributes) }
    end

    factory :user do
      name "Amy"

      location { attributes_for(:location) }

      initialize_with { new(attributes) }
    end
  end

  user = FactoryGirl.build(:user)

  expect(user.name).to eq "Amy"
  expect(user.location.city).to eq "London"
end

:

puts FactoryGirl.attributes_for(:user)
# => {:name=>"Amy", :location=>{:city=>"London"}}

initialize User initialize_with .

Time to lint


FactoryGirl Ruby . Linting , Factory.

FactoryGirl.lint Rake :

FactoryGirl::InvalidFactoryError: The following factories are invalid:
* user - undefined method `save!' for # (NoMethodError)

#save!, #lint . 2 :

: #skip_create

#skip_create Factory:

 FactoryGirl.define do
  factory :location do
    city "London"

    skip_create
    initialize_with { new(attributes) }
  end

  factory :user do
    name "Amy"

    location { attributes_for(:location) }

    skip_create
    initialize_with { new(attributes) }
  end
end 

Factory . #skip_create create :

FactoryGirl.create(:user)

: Lint by building


Rake , factory:

namespace :factory_girl do
  desc "Lint factories by building"
  task lint_by_build: :environment do
    if Rails.env.production?
      abort "Can't lint factories in production"
    end

    FactoryGirl.factories.each do |factory|
      lint_factory(factory.name)
    end
  end

  private

  def lint_factory(factory_name)
    FactoryGirl.build(factory_name)
  rescue StandardError
    puts "Error building factory: #{factory_name}"
    raise
  end
end

Factory:

class User
  attr_reader :name

  def initialize(data)
    @name = data.fetch(:name) # <-  
  end
end

FactoryGirl.define do
  factory :user do
    # name "Amy" <- 

    initialize_with { new(attributes) }
  end
end

factory_girl:lint_by_build Rake :

Error building factory: user
rake aborted!
KeyError: key not found: :name
/path/models.rb:5:in `fetch'
/path/models.rb:5:in `initialize'
/path/factories.rb:8:in `block (3 levels) in '
/path/Rakefile:15:in `lint_factory'

:


FactoryGirl + Ruby:

  • FactoryGirl.build FactoryGirl.create.
  • initialize_with .
  • Factory attributes_for .
  • :
    #skip_create
    linting Factory

P.S. .
Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/332100/

:  

: [1] []
 

:
: 

: ( )

:

  URL