在尝试使用Authlogic运行控制器测试时,出现了“main:Object的undefined method 'setup'”错误。

3

我是一名对RSpec测试有些陌生的人,正在尝试使用RSpec 2和Authlogic 3身份验证在我的Rails 3应用程序中运行一些控制器测试。

按照Authlogic文档提供的步骤(http://rdoc.info/github/binarylogic/authlogic/master/Authlogic/TestCase),我在我的文件中得到了以下代码:

spec_helper.rb

require "authlogic/test_case" # include at the top of test_helper.rb  

events_controller_spec.rb

require 'spec_helper'  
setup :activate_authlogic  

通过运行rake spec SPEC='spec/controllers/eventos_controller_spec.rb'进行测试时,我得到了以下错误:

events_controller_spec.rb:2: undefined method `setup' for main:Object (NoMethodError)

在使用authlogic之前,我运行测试时没有遇到任何问题。

我正在使用Ubuntu 11.04和以下配置:

ruby - 1.8.7  
rails - 3.0.7  
authlogic - 3.0.2  
rspec-rails - 2.4.1  
factory_girl_rails - 1.0.1
1个回答

1

由于它是一个Test::Unit方法,所以设置方法未定义。来自authlogic文档

如果您正在使用Ruby附带的标准测试库Test::Unit::TestCase,则可以跳过下一部分。如果没有,则需要将Authlogic::TestCase包含到您的测试套件中。

要在RSpec中执行相同的操作,您必须包含Authlogic::TestCase并在每个spec之前调用activate_authlogic:

require 'spec_helper'

describe EventsController do
  include Authlogic::TestCase

  before(:each) do
    activate_authlogic
  end
end

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接