开发者

Rspec 2 + Devise + Cancan - Factorygirl = Controller Testing not working

I'm trying to move to rspec for testing, but I can't get controller testing to work with user authentication. Testing a route that doesn't require a user works.

require 'spec_helper'

describe UddsController do
  include Devise::TestHelpers
  render_views

  before (:each) do
    @user = User.new(:email => "test@user.com", :username => "test123")
    @user.roles << Role.find_or_create_by_name("admin")
    @user.save
    sign_in @user
  end

  it "should get index" do
    get :index
    response.should be_success
  end

I just get

  2) UddsController should get index
     Failure/Error: response.should be_success
       expected success? to return true, got false
     # ./spec/controllers/udds_controller_spec.rb:21

I'm not using factory_girl. All the example fixes here and 开发者_运维知识库on google seem to. I don't see why what I've done wouldn't work. Error is useless in debugging.

I know this question has been asked to death. I've read every blog and forum I can find. I'm at a dead end.


It might be redirecting. be_success returns false for any non-2xx status codes (like 302). Try checking the value of response.status -- if it's redirecting, it probably means your authentication or authorization scheme is producing an unexpected result.


Try adding puts response.body before your assertion, and see what the page you are requesting says. If you can't figure it out from there, perhaps edit your question to include the output?


If you are using devise's "confirmable" module which makes users click a link in an email to confirm their address, then when you use "sign_in" in the tests you also have to fake email confirming. It looks something like this:

sign_in @user
@user.confim!
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜