My helper stubs aren't working (rspec2, rails3)

My helper tests broke when I upgraded to rails3/rspec2.3 - I don’t seem
to be able to stub out methods in my helper tests:

#users_helper_spec.rb
describe UsersHelper do
describe “the helper” do
it “should stub” do
helper.stub(:foobar_method).and_return(666)
helper.foobar_method.should == 666
end
end
end

gives me:

  1. UsersHelper the helper should stub
    Failure/Error: helper.foobar_method.should == 666
    undefined method `foobar_method’ for #ActionView::Base:0x4808014

./spec/helpers/users_helper_spec.rb:5

Anyone know what’s going on here?

On Dec 31, 2010, at 1:48 PM, Kevin L. wrote:

 helper.foobar_method.should == 666

end
end
end

gives me:

  1. UsersHelper the helper should stub
    Failure/Error: helper.foobar_method.should == 666
    undefined method `foobar_method’ for #ActionView::Base:0x4808014

./spec/helpers/users_helper_spec.rb:5

I copied that code into a new Rails 3/RSpec 2 app and the example passes
without error.

Can you post your Gemfile and spec/spec_helper.rb files? I’m guessing
there’s a configuration problem of some sort.

Cheers,
David

David C. wrote in post #971734:

Can you post your Gemfile and spec/spec_helper.rb files? I’m guessing
there’s a configuration problem of some sort.

Cheers,
David

David - thanks for following up so quickly, and happy new year.

#Gemfile:
source ‘http://rubygems.org

gem ‘rails’, ‘3.0.3’
gem ‘mysql2’
gem ‘activerecord-import’
gem ‘htmldoc’, ‘0.2.1’
gem ‘moomerman-twitter_oauth’, ‘0.2.1’, :require => ‘twitter_oauth’
gem ‘twitter’, ‘0.4.2’
gem ‘roo’, ‘1.2.3’
gem ‘hashie’
gem ‘ruby-hmac’, :require => ‘hmac-sha2’
gem ‘httparty’
gem ‘ruby-debug’
gem ‘bitly’
gem ‘soap4r’
gem ‘rack-openid’
gem ‘will_paginate’, ‘~> 3.0.pre2’
gem “rake”
gem “hoe”
gem “rcov”
gem “right_http_connection”

group :test do
gem ‘webrat’
gem ‘factory_girl_rails’
gem “rspec”
gem “rspec-mocks”
gem ‘rspec-rails’
gem ‘remarkable’
gem “remarkable_activerecord”
gem “shoulda”
end

#spec/spec_helper.rb
ENV[“RAILS_ENV”] ||= ‘test’
require File.expand_path(“…/…/config/environment”, FILE)
require ‘rspec/rails’
require ‘shoulda’

Requires supporting ruby files with custom matchers and macros, etc,

in spec/support/ and its subdirectories.

Dir[Rails.root.join(“spec/support/**/*.rb”)].each {|f| require f}
include AuthenticatedTestHelper

RSpec.configure do |config|

== Mock Framework

If you prefer to use mocha, flexmock or RR, uncomment the

appropriate line:

config.mock_with :mocha

config.mock_with :flexmock

config.mock_with :rr

config.mock_with :rspec

Remove this line if you’re not using ActiveRecord or ActiveRecord

fixtures
config.fixture_path = “#{::Rails.root}/spec/fixtures”

If you’re not using ActiveRecord, or you’d prefer not to run each of

your

examples within a transaction, remove the following line or assign

false

instead of true.

config.use_transactional_fixtures = true

config.global_fixtures = :users
end

David C. wrote in post #971793:

You only need ^^ rspec-rails here. It requires rspec, which requires
rspec-mocks. I’d actually recommend using this format:

gem “rspec-rails”, “~> 2.3”

This will accept updates up to, but not including 3.0. This means
you’ll get bug fixes and/or new functionality, but no breaking
changes.

thanks, will do.

Unfortunately, I don’t see anything here right out of the gate that
would be causing this.

Would you try bootstrapping a new app and see if the same thing happens?

Unfortunately that worked:

ThingsHelper
supports stubs

. . . so there’s something weird going on with my app.

Any other suggestions before I start wrestling with it?

On Sat, Jan 1, 2011 at 1:12 AM, Kevin L. [email protected]
wrote:

David C. wrote in post #971734:

Can you post your Gemfile and spec/spec_helper.rb files? I’m guessing
there’s a configuration problem of some sort.

Cheers,
David

David - thanks for following up so quickly, and happy new year.

To you as well.

gem ‘hashie’
gem “right_http_connection”

group :test do
gem ‘webrat’
gem ‘factory_girl_rails’
gem “rspec”
gem “rspec-mocks”
gem ‘rspec-rails’

You only need ^^ rspec-rails here. It requires rspec, which requires
rspec-mocks. I’d actually recommend using this format:

gem “rspec-rails”, “~> 2.3”

This will accept updates up to, but not including 3.0. This means
you’ll get bug fixes and/or new functionality, but no breaking
changes.

appropriate line:

If you’re not using ActiveRecord, or you’d prefer not to run each of

your

examples within a transaction, remove the following line or assign

false

instead of true.

config.use_transactional_fixtures = true

config.global_fixtures = :users
end

Unfortunately, I don’t see anything here right out of the gate that
would be causing this.

Would you try bootstrapping a new app and see if the same thing happens?

When I run this I get the following output:

ThingsHelper supports stubs

Finished in 0.0257 seconds
1 example, 0 failures

Kevin L. wrote in post #971805:

David C. wrote in post #971793:

Unfortunately that worked:

ThingsHelper
supports stubs

. . . so there’s something weird going on with my app.

It seems that I left out a critical detail :slight_smile:

I was using incorrect syntax to stub a different method in a before
block:

before do
stub(:some_other_method).and_return(val)
end

Which appears to silently break stubbing. Try the following:

describe ThingsHelper do
it “supports stubs” do
stub!(:bar) {“foobar”}
helper.stub(:foo) { “bar” }
helper.foo.should eq(“bar”)
end
end

that gives me:

  1. ThingsHelper supports stubs
    Failure/Error: helper.foo.should eq(“bar”)
    undefined method `foo’ for #ActionView::Base:0x1cef47c

On Jan 1, 2011, at 2:49 PM, Kevin L. wrote:

supports stubs

. . . so there’s something weird going on with my app.

Any other suggestions before I start wrestling with it?

It’s New Year’s Day. Start with a pint, and then go from there :slight_smile: