如何使用 RSpec 和 Devise/CanCan 进行集成测试? [英] How to do integration testing with RSpec and Devise/CanCan?

查看:16
本文介绍了如何使用 RSpec 和 Devise/CanCan 进行集成测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个 Devise 模型用户,其中只有具有角色 :admin 的用户才能查看某个 url,我如何编写 RSpec 集成测试来检查该 url 的状态是否返回 200?

If I have a Devise model User, of which only those users with role :admin are allowed to view a certain url, how can I write an RSpec integration test to check that the status returns 200 for that url?

def login(user)
  post user_session_path, :email => user.email, :password => 'password'
end

这是在这个问题的答案中伪建议的:在请求规范中存根身份验证,但我一生都无法让它与设计一起工作.CanCan 在检查能力时收到一个 nil 用户,这自然没有正确的权限.

This was pseudo-suggested in the answer to this question: Stubbing authentication in request spec, but I can't for the life of me get it to work with devise. CanCan is receiving a nil User when checking Ability, which doesn't have the correct permissions, naturally.

在集成规范中无法访问控制器,因此我无法存根 current_user,但我想做这样的事情.

There's no access to the controller in integration specs, so I can't stub current_user, but I'd like to do something like this.

describe "GET /users" do
  it "should be able to get" do
    clear_users_and_add_admin #does what it says...
    login(admin)
    get users_path
    response.status.should be(200)
  end
end

注意!!!:自从提出问题以来,这一切都发生了变化.目前最好的方法是:http://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara

NOTE!!!: all this has changed since the question was asked. The current best way to do this is here: http://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara

推荐答案

@pschuegr 自己的回答让我跨越了界限.为了完整起见,这就是我所做的,让我可以轻松地为请求规范和控制器规范进行设置(使用 FactoryGirl 创建用户实例):

@pschuegr's own answer got me across the line. For completeness, this is what I did that gets me easily set up for both request specs and controller specs (using FactoryGirl for creating the user instance):

在/spec/support/sign_in_support.rb:

in /spec/support/sign_in_support.rb:

#module for helping controller specs
module ValidUserHelper
  def signed_in_as_a_valid_user
    @user ||= FactoryGirl.create :user
    sign_in @user # method from devise:TestHelpers
  end
end

# module for helping request specs
module ValidUserRequestHelper

  # for use in request specs
  def sign_in_as_a_valid_user
    @user ||= FactoryGirl.create :user
    post_via_redirect user_session_path, 'user[email]' => @user.email, 'user[password]' => @user.password
  end
end

RSpec.configure do |config|
  config.include ValidUserHelper, :type => :controller
  config.include ValidUserRequestHelper, :type => :request
end

然后在请求规范中:

describe "GET /things" do
  it "test access to things, works with a signed in user" do
    sign_in_as_a_valid_user
    get things_path
    response.status.should be(200)
  end
end

describe "GET /things" do
  it "test access to things, does not work without a signed in user" do
    get things_path
    response.status.should be(302) # redirect to sign in page
  end
end

类似地,在控制器规范中使用signed_in_as_valid_user"(它包装了来自 FactoryGirl 的用户的 Devise::TestHelpers sign_in 方法)

and similarly, use 'signed_in_as_valid_user' in controller specs (which wraps Devise::TestHelpers sign_in method with a user from FactoryGirl)

这篇关于如何使用 RSpec 和 Devise/CanCan 进行集成测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆