1

我有一个 auth 方法,想把它放在我的 application_controller 中。

class ApplicationController < ActionController::Base  
  helper_method :check_cred

  def check_cred
    "within check cred"
  end

但如果我这样做

require 'spec_helper'

describe ApplicationController do
  it 'should check_cred', task050: true do
    check_cred.should == 'within check cred'
  end
end

我得到:

 undefined local variable or method `check_cred' for #<RSpec::Core::ExampleGroup::Nested_9:0x007ff5e3e40558>

我将如何调用这样的方法进行测试?

谢谢

4

1 回答 1

0

RSpec 控制器规范包装了ActionController::TestCase::Behavior,它提供了一些在测试期间使用的实例变量:

特殊实例变量

ActionController::TestCase 还将自动提供以下实例变量以供测试使用:

@controller:将被测试的控制器实例。

因此,您可以执行以下操作:

it 'should check_cred', task050: true do
  @controller.check_cred.should == 'within check cred'
end

或者,您可以将此辅助方法移出到单独的辅助模块中,并使用 RSpec辅助规范来执行测试,这可能被证明是构建此测试的更好方法。

于 2013-06-05T18:45:12.080 回答