配置Warden以在RSpec控制器规格中使用 [英] Configuring Warden for use in RSpec controller specs

查看:107
本文介绍了配置Warden以在RSpec控制器规格中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够使用Devise的sign_in方法登录我的控制器规格中的用户.但是,既然我要从应用程序中删除Devise,我现在还不确定如何单独使用Warden来获得类似的功能.

I was able to use Devise's sign_in method to log in a user in my controller specs. But now that I'm removing Devise from my application, I'm not quite sure how to get similar functionality working with just Warden on its own.

我应该如何设置spec/spec_helper.rb和相关的spec/support/*.rb文件,以使Warden在控制器规格内充分运行?

How should I go about setting up spec/spec_helper.rb and related spec/support/*.rb files to get Warden running within controller specs sufficiently?

我尝试在spec/support/warden.rb上设置具有以下内容的文件:

I've tried setting up a file at spec/support/warden.rb with these contents:

RSpec.configure do |config|
  config.include Warden::Test::Helpers

  config.after do
    Warden.test_reset!
  end
end

然后我有类似的before调用来验证user工厂:

Then I have before calls similar to this to authenticate a user factory:

before { login_as FactoryGirl.create(:user) }

但这是我一直看到的错误:

But here is the error that I keep seeing:

NameError:
  undefined method `user' for nil:NilClass

此错误可追溯到我在控制器中的authenticate_user!方法:

This error traces back to my authenticate_user! method in the controller:

def authenticate_user!
  redirect_to login_path, notice: "You need to sign in or sign up before continuing." if env['warden'].user.nil?
end

我会很感激任何人都可以提供的指导.

I'd appreciate any guidance that anyone could provide.

推荐答案

您要执行的操作存在一个基本问题. Warden是Rack中间件,但是RSpec控制器规范甚至不包括Rack,因为这些类型的规范并不是要运行完整的应用程序堆栈,而只是要运行控制器代码.您可以使用单独的中间件测试来测试中间件,但是在这种情况下,我认为测试Warden本身是否有效没有意义.

There is a basic problem with what you are trying to do. Warden is a Rack middleware, but RSpec controller specs don't even include Rack, as these types of specs are not meant to run your full application stack, but only your controller code. You can test your middleware with separate tests just for those, but in this case, I don't think it makes sense to test that Warden itself works.

要测试您是否正确配置了Warden,应使用请求规格或集成规格(黄瓜,水豚或类似规格).

To test that you have Warden configured correctly, you should use request specs or integration specs (cucumber, capybara or similar).

尽管从技术上讲,可以在控制器规格中模拟Warden,但我认为这样做并不能给您带来太多好处,而同时会大大增加测试代码的复杂性.请记住,Rack中间件旨在以透明的方式运行,因此您可以轻松地随心所欲地交换中间件.实际上,您的控制器完全不应直接依赖于Warden(也许除了ApplicationController以外),因此对控制器的Warden进行测试依赖是封装破裂的迹象.

Although it is technically possible to mock out Warden in controller specs, I think it is not providing you much benefit while increasing the complexity of your test code significantly. Keep in mind that Rack middleware is meant to operate in a transparent way so that it is easy to swap middleware in and out as you like. Your controller should not be directly dependent on Warden at all (except perhaps for ApplicationController), actually, so having a test dependency on Warden for your controller is a sign of broken encapsulation.

我最近也遇到过同样的问题,希望此评论对您有用.

I ran into this same issue recently so I hope this comment will be useful.

这篇关于配置Warden以在RSpec控制器规格中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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