[rspec-users] my helper stubs aren't working (rspec2, rails3)

42 views
Skip to first unread message

Kevin Lochner

unread,
Dec 31, 2010, 2:48:51 PM12/31/10
to rspec...@rubyforge.org
My helper tests broke when I upgraded to rails3/rspec2. I don't seem to
be able to stub out methods in my helper tests.

Anyone know what's going on here?

#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

--
Posted via http://www.ruby-forum.com/.
_______________________________________________
rspec-users mailing list
rspec...@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

David Chelimsky

unread,
Dec 31, 2010, 7:36:30 PM12/31/10
to rspec...@rubyforge.org
On Dec 31, 2010, at 1:48 PM, Kevin Lochner wrote:

> My helper tests broke when I upgraded to rails3/rspec2. I don't seem to
> be able to stub out methods in my helper tests.
>
> Anyone know what's going on here?
>
> #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

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

Kevin Lochner

unread,
Jan 1, 2011, 2:12:21 AM1/1/11
to rspec...@rubyforge.org
David Chelimsky 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 Chelimsky

unread,
Jan 1, 2011, 12:40:48 PM1/1/11
to rspec...@rubyforge.org
On Sat, Jan 1, 2011 at 1:12 AM, Kevin Lochner <li...@ruby-forum.com> wrote:
> David Chelimsky 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.

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.

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?

<script>
gem install rails bundler
rails new example
cd example
echo 'gem "rspec-rails", "~> 2.3", :group => [:development, :test]' >> Gemfile
bundle install
rails generate rspec:install
rails generate scaffold Things name:string
echo 'require "spec_helper"

describe ThingsHelper do
it "supports stubs" do
helper.stub(:foo) { "bar" }
helper.foo.should eq("bar")
end
end
' > spec/helpers/things_helper_spec.rb
rake db:migrate && rake db:test:prepare
rspec spec/helpers/things_helper_spec.rb -cfd
</script>

When I run this I get the following output:

<output>
ThingsHelper
supports stubs

Finished in 0.0257 seconds
1 example, 0 failures
</output>

Kevin Lochner

unread,
Jan 1, 2011, 3:49:37 PM1/1/11
to rspec...@rubyforge.org
David Chelimsky 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?

David Chelimsky

unread,
Jan 1, 2011, 4:06:03 PM1/1/11
to rspec...@rubyforge.org

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

> David Chelimsky 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?

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

Kevin Lochner

unread,
Jan 1, 2011, 4:10:55 PM1/1/11
to rspec...@rubyforge.org
Kevin Lochner wrote in post #971805:

> David Chelimsky 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 :)

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>

Reply all
Reply to author
Forward
0 new messages