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.
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.
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