Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rspec helper.stub doesn't work

I'm developing Rails 3.1.1, Ruby 1.9.2 and testing with Rspec2.
Stubbing in helper doesn't work.

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  

error

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)>'

Stubbing in controller and view works properly, but in helper it doesn't work.
Any ideas?
Thanks in advance.

like image 535
DIGITALSQUAD Avatar asked Apr 15 '26 03:04

DIGITALSQUAD


1 Answers

I've solved.

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 works fine.

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

like image 113
DIGITALSQUAD Avatar answered Apr 23 '26 05:04

DIGITALSQUAD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!