[RSpec] When you want to use the instance variable of the controller in the test [assigns is not recommended]

Starting with Rails 5, assigns (also assert_template) has been deprecated. For the time being, the controller test itself is not deprecated, so assigns can be used, but you need to install a gem called rails-controller-testing.

However, this is just a remedy for existing projects and it is not recommended to use assigns in new projects. (I don't know why it's so crazy. Please let me know if you know: pray :)

instead of,

controller.instance_variable_get("@hoge")

You can get an instance variable by using.

Code example

I have excerpted a part of the password reset process test I wrote recently.

password_resets_request_spec.rb


RSpec.describe 'PasswordResets', type: :request do
  let(:user) { create(:user) }

 describe 'Access to password reset edit screen' do
    context 'When the email address is incorrect' do
      it 'user should be redirected to the password reset page' do
        post password_resets_path, params: { password_reset: { email: user.email } }
        user = controller.instance_variable_get('@user')
        get edit_password_reset_path(user.reset_token, email: '')
        expect(flash[:danger]).to be_truthy
        follow_redirect!
        expect(request.fullpath).to eq '/password_resets/new'
      end
    end

In the code above, I wanted to access the reset_token attribute, so it feels like I'm getting an instance variable called @user for the create action.

Thank you for reading to the end!

I've been desperate to deploy personally developed apps lately and it's a bit short: pray: Once the deployment is complete, I'll write another article! !! Thank you.

Recommended Posts

[RSpec] When you want to use the instance variable of the controller in the test [assigns is not recommended]
ProxyFactory is convenient when you want to test AOP in Spring!
When you want to use the method outside
You may not want to use the remove method in ArrayList very often
If you want to recreate the instance in cloud9
When you want to change the MySQL password of docker-compose
Delegate is convenient to use when you want to reuse parts
Use JLine when you want to handle keystrokes on the console character by character in Java
[Swift] When you want to know if the number of characters in a String matches a certain number ...
What to do when you want to know the source position where the method is defined in binding.pry
If you want to mock a method in RSpec, you should use the allow method for mock and the singleton method.
A memo when you want to clear the time part of the calendar
How to constrain the action of the transition destination when not logged in
I want you to use Enum # name () for the Key of SharedPreference
What to do if Operation not permitted is displayed when you execute a command in the terminal
[Rails + Webpacker] I want to use images of assets! Until you can view the image in Vue.js
If you do not call shutdownNow when the transfer is completed in the Java SDK of AWS S3, threads will continue to remain
How to use "sign_in" in integration test (RSpec)
When you want to bind InputStream in JDBI3
[Swift] Use nonzeroBitCount when you want popcnt in Swift
How to set when "The constructor Empty () is not visible" occurs in junit
Is it mainstream not to write the closing tag of <P> tag in Javadoc?
When the project is not displayed in eclipse
What to do when ‘Could not find’ in any of the sources appears in the development environment with Docker × Rails × RSpec
Summary of copy and paste commands used when you want to delete the cache in iOS application development anyway
When changing the Controller of Spring Web MVC to kotlin, @Autowired The specified component is not injected and becomes null.
[Ruby] When you want to assign the result obtained by conditional branching to a variable and put it in the argument
I want you to put the story that the error was solved when you stabbed the charger in the corner of your head
Rspec: I want to test the post-execution state when I set a method on subject
[rails] After option useful when you want to change the order of DB columns
For those who want to use MySQL for the database in the environment construction of Rails6 ~.
Summary of how to use the proxy set in IE when connecting with Java
Code to use when you want to process Json with only standard library in Java
When the hover of Eclipse is hard to see
When you want to dynamically replace Annotation in Java8
How to solve the problem when the value is not sent when the form is disabled in rails and sent
When rewriting the CMD of docker image of Pod in the manifest of k8s, command is not good
What to do when Rails on Docker does not reflect controller changes in the browser
When you want to reflect the Master Branch information in the Current Branch you are currently working on
The idea of cutting off when the error is not resolved
[Rails] When the layout change of devise is not reflected
A collection of patterns that you want to be aware of so as not to complicate the code
Comparison of version strings (Java implementation) when you want to branch the process between two versions
A memorandum when you want to see the data acquired by Jena & SPARQL for each variable.
[Note] What to do if bundle install in Chapter 3 of the rails tutorial is not possible
How to write when you want to handle "array of C language strings" like argv [] in Ruby-FFI
If you just want to run your containers in the cloud, Azure Container Instances is easy
In order not to confuse the understanding of getters and setters, [Do not use accessors for anything! ]
I want to make the frame of the text box red when there is an input error
About the solution to the problem that the log of logback is not output when the web application is stopped
When you want to check whether the contents of a property can be converted to a specific type
Is it easy for the user to use when implementing general-purpose functions? Let's be aware of
I want to know the Method of the Controller where the Exception was thrown in the ExceptionHandler of Spring Boot
[Rails / Routing] How to refer to the controller in the directory you created
What to do when the changes in the Servlet are not reflected
If you want to include the parent class in Lombok's @builder
When you want to ZIP download the image data saved locally
Want to know what Ruby n is the power of 2? (Power judgment of 2)
How to output the value when there is an array in the array
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
I want to change the value of Attribute in Selenium of Ruby