请输入您要查询的百科知识:

 

词条 Factory Bot (Rails Testing)
释义

  1. Factories

      Defining Factories  

  2. Features

      Traits    Alias    Sequences    Inheritance    Callback  

  3. See also

      Other Test libraries for Ruby  

  4. References

{{Orphan|date=February 2018}}

Factory Bot, originally known as Factory Girl,[1] is a software library for the Ruby programming language that provides factory methods to create test fixtures for automated software testing. The fixture objects can be created on the fly; they may be plain Ruby objects with a predefined state, ORM objects with existing database records or mock objects.

Factory Bot is often used in testing Ruby on Rails applications; where it replaces Rails' built-in fixture mechanism. Rails' default setup uses a pre-populated database as test fixtures, which are global for the complete test suite. Factory Bot, on the other hand, allows developers to define a different setup for each test and thus helps to avoid dependencies within the test suite.[2][3]

Factories

Defining Factories

A factory is defined by a name and its set of attributes. The class of the test object is either determined through the name of the factory or set explicitly.[4]

 FactoryBot.define do   # Determine class automatically   factory :user do     name = "Captain Minion"     superhero false   end      # Specific class explicitly   factory :superhero, class: User do     name = "Tony Stark"     superhero true   end end

Features

Traits

Traits allow grouping of attributes which can be applied to any factory.[4][5]

factory :status do

  title "Seeking for Full Time jobs"   trait :international do    international true  end   trait :resident do    international false  end   trait :comp_sci do    comp_sci true  end   trait :electrical do    comp_sci false  end 	  factory :comp_sci_international_student,  traits: [:international, :comp_sci]  factory :electrical_resident_student,  traits: [:resident, :electrical]

end

Alias

Factory Bot allows creating aliases for existing factories so that the factories can be reused.[4]

factory :user, aliases: [:student, :teacher] do

end

factory :notice do

  teacher  # Alias used ''teacher'' for ''user''  title "Office Hours" end 

factory :notification do

  student  #Alias used student for user   title "Lecture timings"

end

Sequences

Factory Bot allows creating unique values for a test attribute in a given format.[4]

 FactoryBot.define do   factory :title do     sequence(:name){|n| "Title #{n}" }     # Title 1, Title 2 and so on...   end end

Inheritance

Factories can be inherited while creating a factory for a class. This allows the user to reuse common attributes from parent factories and avoid writing duplicate code for duplicate attributes. Factories can be written in a nested fashion to leverage inheritance.

 '''factory''' :user '''do'''    name "Micheal"     '''factory''' :admin '''do'''        admin_rights true    '''end''' '''end'''  admin_user = create(:admin) admin_user.name   ''# Micheal'' admin_user.admin_rights   ''# true''

Parent factories can also be specified explicitly.

 '''factory''' :user '''do'''   name "Micheal" '''end'''  '''factory''' :admin, parent: :user '''do'''   admin_user true '''end'''

Callback

Factory Bot allows custom code to be injected at four different stages:[4]

{{code|after(
build)}}: Code can be injected after the factory is built
{{code|before(
create)}}: Code can be injected before the factory is saved
{{code|after(
create)}}: Code can be injected after the factory is saved
{{code|after(
stub)}}: Code can be injected before the factory is stubbed

See also

Other Test libraries for Ruby

  • NullDB: a way to speed up testing by avoiding database use.
  • [https://github.com/rdy/fixture_builder Fixture Builder]: a tool that compiles Ruby factories into fixtures before a test run.
  • [https://www.jetbrains.com/ruby/help/shoulda.html Shoulda]: an extension to test/unit with additional helpers, macros, and assertions.
  • Rspec: a behavior-driven development framework
  • [https://www.ruby-toolbox.com/projects/capybara Capybara]: Acceptance test framework for web applications.

References

1. ^{{cite web|title=Project Naming History|website=github.com/thoughtbot/factory_bot|url=https://github.com/thoughtbot/factory_bot/blob/master/NAME.md}}
2. ^{{cite web|title=Waiting For a FactoryGirl|website=Thoughtbot Blog|url=https://robots.thoughtbot.com/waiting-for-a-factory-girl}}
3. ^{{cite web|title=Rails Testing|website=hiringthing.com|url=http://www.hiringthing.com/2012/08/17/rails-testing-factory-girl.html}}
4. ^{{cite web|title=Getting Started|website=rubydoc.info/gems/factory_bot|url=http://www.rubydoc.info/gems/factory_bot/file/GETTING_STARTED.md}}
5. ^{{cite web|title= Traits|website=Thoughtbot Blog|url=https://robots.thoughtbot.com/remove-duplication-with-factorygirls-traits}}

1 : Ruby (programming language)

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/9/25 6:31:07