Rails Tutorial Ch. 9 Exercise 6: The expected response will be “redirecting”, but was <200>

I am trying to write tests and application code to redirect users who are already logged in to root_path if they try to CREATE a user or visit a new user path.

Here are the tests I wrote in user_pages_spec.rb:

 describe "for signed in users" do let(:user) { FactoryGirl.create(:user) } before { sign_in user } describe "using a 'new' action" do before { get new_user_path } specify { response.should redirect_to(root_path) } end describe "using a 'create' action" do before { post users_path } specify { response.should redirect_to(root_path) } end end 

UsersController:

 class UsersController < ApplicationController before_action :unsigned_in_user, only: [:create, :new] def new @user = User.new end def create @user = User.new(user_params) if @user.save sign_in @user flash[:success] = "Welcome to the Sample App!" redirect_to @user else render 'new' end end private # Before filters def user_params params.require(:user).permit(:name, :email, :password, :password_confirmation) end def unsigned_in_user puts signed_in? redirect_to root_url, notice: "You are already signed in." unless !signed_in? end end 

puts signed_in? returns false. I guess this is a problem because I expect it to return. Here are the errors after running the tests using rspec. Any help is appreciated.

 Failures: 1) User pages for signed in users using a 'create' action Failure/Error: before { post users_path } ActionController::ParameterMissing: param not found: user # ./app/controllers/users_controller.rb:52:in `user_params' # ./app/controllers/users_controller.rb:20:in `create' # ./spec/requests/user_pages_spec.rb:162:in `block (4 levels) in <top (required)>' 2) User pages for signed in users using a 'new' action Failure/Error: specify { response.should redirect_to(root_path) } Expected response to be a <redirect>, but was <200> # ./spec/requests/user_pages_spec.rb:158:in `block (4 levels) in <top (required)>' 

In session_helper.rb file:

 def signed_in? !current_user.nil? end 

In spec / support / utilities.rb:

 def sign_in(user, options={}) if options[:no_capybara] # Sign in when not using Capybara. remember_token = User.new_remember_token cookies[:remember_token] = remember_token user.update_attribute(:remember_token, User.encrypt(remember_token)) else visit signin_path fill_in "Email", with: user.email fill_in "Password", with: user.password click_button "Sign in" end end 
+6
source share
2 answers

Could you pass the tests?

If you didn’t do this, I had the same problem as you have today, and I managed to pass the tests by performing two test changes - passing the user hash during POSTing and using no_capybara in the sign_in method, since get and post are not capybara methods, and I think that RSpec does not behave as we would expect if we switch from capybara to non-capybara methods within the same test.

 describe "for signed-in users" do let(:user) { FactoryGirl.create(:user) } before { sign_in user, no_capybara: true } describe "using a 'new' action" do before { get new_user_path } specify { response.should redirect_to(root_path) } end describe "using a 'create' action" do before do @user_new = {name: "Example User", email: " [email protected] ", password: "foobar", password_confirmation: "foobar"} post users_path, user: @user_new end specify { response.should redirect_to(root_path) } end end 
+4
source

Same answer as najwa, but I used a FactoryGirl user with a Rails attribute to avoid duplication:

 describe "using a 'create' action" do before { post users_path, user: user.attributes } specify { response.should redirect_to(root_path) } end 

Helps disable data from test code.

+3
source

Source: https://habr.com/ru/post/951008/


All Articles