Rspec: I want to test the post-execution state when I set a method on subject

Premise

context "Generate user" do
  subject { User.create(name: "John") }
  it "One user is generated" do
    is_expected.to change(User.count).by(1)
  end
end

As subject, I defined a method for user generation. In this situation, I sometimes wanted to test with the same subject that the generated username was "John".

If it doesn't work

it "Username is John" do
  subject
  expect(User.last.name).to eq("John")
end

Even so, I got an error saying that User.last is nil, and it seems that the contents of the subject are not being executed.

Cause investigation

puts subject.class

If you look up the class as

Proc

Will be returned.

In ruby, there is a class called Proc class that stores the execution code itself in a variable.

Therefore

subject

Even if you write as

"String"

It's the same as writing, and nothing happens. Therefore, it is necessary to execute it as an object of Proc class.

Solutions

There are various ways to do it, but for example

subject.call

there is

Therefore

it "Username is John" do
  subject.call
  expect(User.last.name).to eq("John")
end

This passed.

It may be in a different context, or the subject setting may not be good, but if you face the same situation, please consider it.

Whole code

context "Generate user" do
  subject { User.create(name: "John") }

  it "One user is generated" do
    is_expected.to change(User.count).by(1)
  end

  it "Username is John" do
    subject.call
    expect(User.last.name).to eq("John")
  end
end

that's all.

Recommended Posts

Rspec: I want to test the post-execution state when I set a method on subject
I want to call a method and count the number
I want to write a unit test!
I want to test Action Cable with RSpec test
[Ruby] I want to do a method jump!
When you want to use the method outside
I want to play a GIF image on the Andorid app (Java, Kotlin)
I want to call a method of another class
I want to call the main method using reflection
I want to click a GoogleMap pin in RSpec
[Rough commentary] I want to marry the pluck method
I want to simplify the log output on Android
I want to add a delete function to the comment function
I want to go back to a specific VC by tapping the back button on the NavigationBar!
[Android Studio] I want to set restrictions on the values registered in EditText [Java]
I want to hit the API with Rails on multiple docker-composes set up locally
I want to set devise_parameter_sanitizer individually when I create two devises
I want to randomly generate information when writing test code
I want to set the conditions to be displayed in collection_check_boxes
I want to use screen sharing on the login screen on Ubuntu 18
I want to expand the clickable part of the link_to method
I want to create a form to select the [Rails] category
I want to use the sanitize method other than View.
How to test a private method with RSpec for yourself
I want to put the JDK on my Mac PC
I want to give a class name to the select attribute
I want to transition to the same screen in the saved state
[Ruby on Rails] I get a warning when executing RSpec due to a different gem version.
Implemented a strong API for "I want to display ~~ on the screen" with simple CQRS
I want to create a chat screen for the Swift chat app!
I want to add a browsing function with ruby on rails
I want to use swipeback on a screen that uses XLPagerTabStrip
A note when I was addicted to converting Ubuntu on WSL1 to WSL2
I tried to explain the method
I want to add the disabled option to f.radio_button depending on the condition
[Java] I tried to make a maze by the digging method ♪
If you want to mock a method in RSpec, you should use the allow method for mock and the singleton method.
[RSpec] When you want to use the instance variable of the controller in the test [assigns is not recommended]
I want to download a file on the Internet using Ruby and save it locally (with caution)
A memo when you want to clear the time part of the calendar
A story I was addicted to when testing the API using MockMVC
I want to know the JSP of the open portlet when developing Liferay
I can't build if I set the build destination to a simulator with XCode12!
I want to pass the argument of Annotation and the argument of the calling method to aspect
I want to get the IP address when connecting to Wi-Fi in Java
I want to display an error message when registering in the database
How to reference a column when overriding the column name method in ActiveRecord
I want to develop a web application!
I want to write a nice build.gradle
I want to display background-ground-image on heroku.
I want to RSpec even at Jest!
I want to install PHP 7.2 on Ubuntu 20.04.
I wanted to add @VisibleForTesting to the method
I was addicted to the roll method
I was addicted to the Spring-Batch test
When I wanted to create a method for Premium Friday, it was already in the Java 8 standard API
[Ruby] I want to make an array from a character string with the split method. And vice versa.
What I tried when I wanted to get all the fields of a bean
[Rails] When transitioning to a page with link_to, move to the specified location on the page
[RSpec] What I got stuck when I wrote the mailer (password reset process) test
[When using MiniMagick] A memorandum because I stumbled in the CircleCI test environment.