[RUBY] [RSpec] Let's use FactoryBot [Rails]

What is Factory Bot? ??

--A library that makes it easy to create data for testing -- ĽUse Active Record`

This and that of Factory Bot

If you install factory_bot_rails, you can create a file to create factory with the following command.

bin/rails g factory_bot:model task

I will explain a little.

Up to bin / rails g should be fine. Explicitly appear in the model with factory_bot: model. Please specify the target like task.

When you run this command, you should see a file like the following in spec / factories / tasks.rb.

spec/factories/tasks.rb


FactoryBot.define do

  factory :task do

  end
end

You have a template. Let's define the data in this.

spec/factories/tasks.rb


FactoryBot.define do

  factory :task do
    name "Shopping"
    content "Go buy supper"
  end
end

It may be less, but it should be okay. By executing FactoryBot.create (: task) in the test file, you can create the data described in spec / factories / tasks.rb.

Use FactoryBot a little.

Override

For example, if you want to write an abnormal test that outputs an error when name of task is nil, you can write as follows.

spec/models/task_spec.rb


#Invalid if there is no name.
it "is invalid without a name" do
  task = FactoryBot.build(:task, name: nil)
  task.valid?
  expect(task.errors[:name]).to include("can't be blank")
end

By overwriting the property to nil, it looks as expected.

Handle unique values.

For example, the email address should be unique, it has such a specification, and you should write such a test. However, FactoryBot always refers to the same value, so it may not be possible to handle such an event.

This problem can be solved by using ** Sequence ** as shown below.

spec/factories/users.rb


FactoryBot.define do

  factory :user do
    name "Yamada"
    sequence(:email) { |n| "test#{n}@example.com" }
  end
end

By defining it like this, it will be set as [email protected], [email protected].

Very convenient.

Recommended Posts

[RSpec] Let's use FactoryBot [Rails]
Let's unit test with [rails] Rspec!
Let's use jcmd
Rspec Basics [Rails]
[Rails5] Rspec -validation-
[Rails] Use jQuery
About RSpec (Rails)
[Rails] Test with RSpec
Use webmock with Rspec
Use images in Rails
Rails, RSpec installation procedure
[Swift] Let's use extension
[Rails / RSpec] Write Model test (using Shoulda Matchers / FactoryBot)
I tried unit testing Rails app using RSpec and FactoryBot
[Rails] How to use enum
[RSpec] Let's master Factory Bot
[Swift] Let's use Segmented Control
[Rails] How to use enum
[spring] Let's use Spring Data JPA
Let's use Swift Firebase Firebase Auth
How to use rails join
[Rails] Test code using Rspec
Use multiple databases with Rails 6.0
[Rails] How to use validation
[Rails] About Rspec response test
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
Use multiple checkboxes in Rails6!
[Docker] Use whenever with Docker + Rails
[Rails] How to use Scope
[Rails] How to use gem "devise"
[Rails] How to use flash messages
Let's use Set more [Collections Framework]
Rails book review app RSpec introduction
How to use Ruby on Rails
Let's use Twilio in Java! (Introduction)
Let's use Amazon Textract with Ruby
Let's use reflection Private method access
[Rails] Use cookies in API mode
Let's use it after understanding sudo!
[Rails5] Rspec -Unit test when nesting-
[Rails] RSpec Kihon no Ki [Introduction]
[Rails] How to use Active Storage
[Introduction to Rails] How to use render
[RSpec] Unit test (using gem: factory_bot)
[For Rails beginners] Summary of how to use RSpec (get an overview)