Saturday, 14 September 2013

Test including redirect_to fails with rspec but application is working as expected. Chapter 9.6.6 of Michael Hartl book (v 4.0)

Test including redirect_to fails with rspec but application is working as
expected. Chapter 9.6.6 of Michael Hartl book (v 4.0)

I'm trying to solve a problem 6 of chapter 9.6 of Michael Hartl book Rails
v 4.0.
Though everything looks fine in rspec test:
describe "should redirect a logged in user to root url if user tries to
hit new in users controller" do
let(:user) {FactoryGirl.create(:user)}
before do
sign_in user
get new_user_path
end
specify{expect(response).to redirect_to(root_url)}
end
describe "should redirect a logged in user to root url if user tries to
hit create in users controller" do
let(:params) do {user: {name: "Tester", email: "test@example.com",
password: "password",
password_confirmation: "password"}}
end
let(:user) {FactoryGirl.create(:user)}
before do
sign_in user
post users_path, params
end
specify{expect(response).to redirect_to(root_url)}
end
And here is my controller snippet:
class UsersController < ApplicationController
before_action :is_signed_in, only: [:new, :create]
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
def is_signed_in
redirect_to(root_url) if signed_in?
end
end
sign_in method in utilities.rb
def sign_in(user, options = {})
if options[:no_capybara]
# Signin 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
But my testcases are failing with following errors:
Failures:
1) Authentication signin with valid information should redirect a logged
in user to root url if user tries to hit create in users controller
Failure/Error: specify{expect(response).to redirect_to(root_url)}
Expected response to be a redirect to <http://www.example.com/> but was
a redirect to <http://www.example.com/users/2>.
Expected "http://www.example.com/" to be ===
"http://www.example.com/users/2".
# ./spec/requests/authentication_pages_spec.rb:69:in `block (5 levels) in
<top (required)>'
2) Authentication signin with valid information should redirect a logged
in user to root url if user tries to hit new in users controller
Failure/Error: specify{expect(response).to redirect_to(root_url)}
Expected response to be a <redirect>, but was <200>
# ./spec/requests/authentication_pages_spec.rb:55:in `block (5 levels) in
<top (required)>'
Finished in 5.22 seconds
95 examples, 2 failures
Failed examples:
rspec ./spec/requests/authentication_pages_spec.rb:69 # Authentication
signin with valid information should redirect a logged in user to root url
if user tries to hit create in users controller
rspec ./spec/requests/authentication_pages_spec.rb:55 # Authentication
signin with valid information should redirect a logged in user to root url
if user tries to hit new in users controller
Randomized with seed 58509
I tried to debug, but not able to figure out exact problem. Can anyone
help me on this ?

No comments:

Post a Comment