문제

I'm using Devise 1.0.8 on a Rails 2.3.8 project. In my controller test I have:

context "when current_user is admin" do
  should "render" do
    sign_in Factory(:admin)
    get :index
    assert_response :success
  end
end

And in the controller itself, there's the following before_filter:

def redirect_if_not_admin
  puts current_user
  redirect_to root_path unless current_user.try(:admin?)
end

This is the output from my test:

true
false

NoMethodError: undefined method `admin?' for true:TrueClass

So current_user is true when I use sign_in in the test. However when I don't, current_user is nil, so it's definitely being affected. Thanks in advance for any help.

도움이 되었습니까?

해결책

I used the confirm! method on the resource in an after_create/build instead of setting the attributes in the factory and that did the trick. current_user equals true when there is an unconfirmed user on the site (someone who has signed up but has yet to click on the confirmation link sent to them).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top