我有一个Rails应用程序,如果没有用户登录,它会自动从主页重定向到登录页面。我使用Authlogic进行身份验证,Cucumber和Capybara进行测试。
我有以下情况:
Scenario: Start application
Given I am not logged in
When I go to the application web page
Then I should see a login form
通过以下测试步骤:
Given(/^I am not logged in$/) do
end
When(/^I go to the application web page$/) do
visit root_path
end
(Then I should see a login form
有点复杂,因为它是通配符,但我没有那么远,所以它可能并不重要。
按照http://laserlemon.com/blog/2011/05/20/make-authlogic-and-cucumber-play-nice/的指导,我创建了一个包含以下内容的文件features\support\authlogic,rb
require 'authlogic/test_case'
World(Authlogic::TestCase)
ApplicationController.skip_before_filter :activate_authlogic
Before do
activate_authlogic
end
但是当我运行Cucumber时,visit root_path
以Authlogic::Session::Activation::NotActivatedError: You must activate the Authlogic::Session::Base.controller with a controller object before creating objects
我做错了什么?
答案 0 :(得分:0)
我一直在使用与rails3完全相同的代码,今天当我尝试使用rails4运行黄瓜测试时,我发现完全相同的错误Authlogic::Session::Activation::NotActivatedError: You must activate the Authlogic::Session::Base.controller with a controller object before creating objects
。
帮助我的是删除行World(Authlogic::TestCase)
和ApplicationController.skip_before_filter :activate_authlogic
并仅在support/authlogic.rb
中保留这一行:
require "authlogic/test_case"
Before do
activate_authlogic
end
找到此解决方案here。现在它可以工作,我稍后会检查是否有任何副作用。