rspec helper.stub不起作用

时间:2011-12-07 10:35:29

标签: rspec rspec2 rspec-rails

我正在开发Rails 3.1.1,Ruby 1.9.2并使用Rspec2进行测试 帮助者的唠叨不起作用。

users_helper_spec.rb

require 'spec_helper'

describe UsersHelper do
  describe 'test' do
    before do
      helper.stub(:val).and_return('this is test')  
    end

    it 'returns val' do
      test.should eql 'this is test'
    end
  end
end  

user_helper.rb

module UsersHelper
  def test
    return val
  end
end  

错误

1) UsersHelper test test
   Failure/Error: test.should eql 'this is test'
   NameError:
     undefined local variable or method `val' for #<RSpec::Core::ExampleGroup::Nested_15::Nested_1:0x007f9ad5f42a50>
   # ./app/helpers/users_helper.rb:3:in `test'
   # ./spec/helpers/users_helper_spec.rb:10:in `block (3 levels) in <top (required)>'

控制器和视图中的存根工作正常,但在帮手中它不起作用 任何想法?
提前谢谢。

1 个答案:

答案 0 :(得分:4)

我已经解决了。

require 'spec_helper'

describe UsersHelper do
  describe 'test' do
    before do
      helper.stub(:val).and_return('this is test')  
    end

    it 'returns val' do
      helper.test.should eql 'this is test'
      # helper. is required.
    end
  end
end

helper.test工作正常。

#199: Can't stub method calls in helpers - Issues - rspec/rspec-rails - GitHub