Why are my RSpec tests failing, but my app is working?

▼魔方 西西 提交于 2019-12-01 03:33:47
Michael K Madison

I had the same issue and found the answer posted here.

basically RSPEC needs both @current_user and current user to be set for sign_in and sign_out. Weird and annoying, but perhaps the reason one might consider using Devise in a production app!

Providing your controller and test code would be helpful. But without them my guess would be that you are missing this in the before block for your failing tests:

@user = Factory(:user)
test_sign_in(@user)

The redirection is happening because of whatever authorization logic is being used in the tutorial, i think it is detailed in 10.11 and 10.12.

This is a common gotcha i see in controller tests with auth, hopefully that's all that it is for you as well.

EDIT:

Thanks for providing your code.

Here's a few things i noticed:

1.) Failures 1-14, 19 are due to the user authentication not working, specifically signed_in?. Are your session controller tests passing? You should definitely make sure those pass before testing the users controller since it depends on that functionality.

2.) Failures 15-18 are because you probably didn't create the migration to add the boolean attribute in Listing 10.35 -or- you need to run the migration on your test database: rake db:test:prepare

Sorry i couldn't pin point all your problems but hopefully gives you a step in the right direction.

Check that deny_access in SessionsHelper is defined before the private block.

The book has a typo that implies that you should put deny_access at the end of the module. Unfortunately this means that the function is defined as private.

module SessionsHelper
  .
  .
  .
  def deny_access
    redirect_to signin_path, :notice => "Please sign in to access this page."
  end
end

The corrected code on the Railstutorial.org website is below:

module SessionsHelper
  .
  .
  .
  def deny_access
    redirect_to signin_path, :notice => "Please sign in to access this page."
  end

  private
  .
  .
  .
end

I had the same problem and using Rails 3.1.3. This is my solution.

First, make sure both:

def current_user=(user)
  @current_user = user
end

def current_user
  @current_user ||= user_from_remember_token
end

are in sessions_helper.rb

Also, in sessions_helper.rb change:

def sign_out
    cookies.delete(:remember_token)
    current_user.nil
end

to

def sign_out
    cookies.delete(:remember_token)
    self.current_user.nil
end

One more thing.

In spec_helper.rb change:

def test_sign_in(user)
    controller.sign_in(user)
end

to

def test_sign_in(user)
    controller.current_user = user
end

See if that helps, I haven't completed the chapter, but this gets me through 10.2.1. Someone with more experience can comment on why this works. I read another solution that wrote around depending on before_filter to get rspec to pass.

Do you have render_views call after top level describe in the users_controller_spec?

I ran into this same problem today using rails 3.1.3. However, I went to the github site for the railstutorial sample app and noticed the sessions_helper.erb there had some changes.

1.) Add the 'self' key word to 'current_user' in the sign_in(user) method:

def sign_in(user)
  cookies.permanent.signed[:remember_token] = [user.id, user.salt]
  self.current_user = user
end

2.) This also goes for the 'sign_out' method, add the 'self' keyword to 'current_user'

def sign_out
  cookies.delete(:remember_token)
  self.current_user = nil
end

I made no other changes and all tests pass.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!