RSpec Rails登录过滤器

| 我最近切换开始在我的Rails(3.0.8)应用程序中使用rspec-rails(2.6.1)。我曾经使用过Test :: Unit,但似乎无法为我的测试方法使用过滤器。我希望将事情保持为DRY,所以我想设置一个过滤器,以便可以在调用测试方法之前以Authlogic用户身份登录的任何测试方法上调用。我尝试通过在spec_helper.rb中使用RSpec过滤器来完成此操作:
config.before(:each, :login_as_admin => true) do 
  post \"/user_sessions/create\", :user_session => {:username => \"admin\", :password => \"admin\"}   
end
然后,在相应的测试方法中使用它(在本例中为spec / controllers / admin_controller_spec.rb):
require \'spec_helper\'

describe AdminController do  
  describe \"GET index\" do        
    it(\"gives a 200 response when visited as an admin\", :login_as_admin => true) do   
      get :index
      response.code.should eq(\"200\")
    end    
  end
end
但是,运行rspec spec时出现此错误:
Failures:

  1) AdminController GET index gives a 200 response when visited as an admin
     Failure/Error: Unable to find matching line from backtrace
     RuntimeError:
       @routes is nil: make sure you set it in your test\'s setup method.
布莱克每次测试只能发送一个HTTP请求吗?我还尝试了我的authenticate_admin方法(位于config.before块中),没有任何运气。     
已邀请:
        不幸的是,目前无法在全局定义的“ 3”钩子中执行您要尝试的操作。原因是
before
钩子以它们被注册的顺序执行,并且在ѭ5declared中声明的那些钩子在
rspec-rails
内部注册的钩子之前被注册,以建立控制器,请求,响应等。 另外,这已报告到https://github.com/rspec/rspec-rails/issues/391。     
        您应该使用shulda的宏。要使用shoulda修改您的spec_helper.rb
RSpec.configure do |config|
  config.include Clearance::Shoulda::Helpers
end
然后可以在控制器规格中设置过滤器
require \'spec_helper\'

describe AdminController do  
  fixture :users

  before(:each) do
    sign_in_as users(:your_user)
  end
  describe \"GET index\" do        
    it(\"gives a 200 response when visited as an admin\", :login_as_admin => true) do   
      get :index
      response.code.should eq(\"200\")
    end    
  end
end
    

要回复问题请先登录注册