如何使用RSpec和Devise / CanCan进行集成测试? [英] How to do integration testing with RSpec and Devise/CanCan?
问题描述
如果我有一个Devise模型用户,其中只有那些具有角色:admin的用户可以查看某个url,那么如何编写一个RSpec集成测试来检查该URL的状态是否返回200?
def login(user)
post user_session_path,:email => user.email,:password => 'password'
end
这个问题的答案是伪建议的: a href =http://stackoverflow.com/questions/5787409/stubbing-authentication-in-request-spec>请求规范中的Stubbing身份验证,但我不能让我的生活得到与设计工作。 CanCan在检查能力时收到一个无效的用户,自然没有正确的权限。
集成规范中无法访问控制器,所以我可以' t stub current_user,但我想做这样的事情。
描述GET / usersdo
它应该能够得到do
clear_users_and_add_admin #does它说...
登录(管理)
get users_path
response.status.should be(200)
end
end
注意!!! :所有这一切都已经改变了,因为问题被提出。目前最好的方式是: http://github.com/plataformatec / devise / wiki / How-To:-Test-with-Capybara
@ pschuegr自己的答案让我跨线为了完整性,这是我所做的,让我轻松地设置了请求规格和控制器规范(使用FactoryGirl创建用户实例):
in / spec /支持/ sign_in_support.rb:
#module帮助控制器规范
模块ValidUserHelper
def signed_in_as_a_valid_user
@user || = FactoryGirl.create:user
sign_in @user#方法from devise:TestHelpers
end
end
#module for help request specs
模块ValidUserRequestHelper
#用于请求规范
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 => :请求
end
然后在请求规范中:
描述GET / thingsdo
它测试访问的东西,与登录用户一起工作do
sign_in_as_a_valid_user
get things_path
response.status.should be(200)
end
end
描述GET / thingsdo
它测试访问事物,不工作没有登录用户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方法)
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
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.
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
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'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):
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
Then in request spec:
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
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屋!