开发者

Can devise sign_in() test helper accept a new_record?

In the following example, can I use build() instead of create()?

class UsersControllerTest < ActionController::TestCase  

  setup do
    @user = Factory.create(:user)
  end

  test "admin can get index" do
    sign_in @user
    get :index
    assert_response :success
  end

  test "user cannot get index" do
    sign_in @user
    get :index
    assert_response 403
  end

end

In real-life use, the user would have already been created (saved) before signing in, so that's why my test uses create(). However, I want to use build() because I hope it will make my tests faster.

The devise README does not explicitly say that it is OK to use buil开发者_开发问答d().


It looks like the user record must be persisted, according to this note by José.

A mock is never going to work. When you say sign in, the user is stored in session (basically, the user class and its id). When you access the controller, another user object is retrieved based on the stored data. The best way to solve the problem is using something that persists the object, like Factory Girl.

https://github.com/plataformatec/devise/issues/928

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜