Controller stub and helper spec with lastest beta

Hello,

I have two issues with beta 2.0.0.beta.18. First, here’s the gems :

gem “rails”, “3.0.0.beta4”
gem “mongoid”, “2.0.0.beta10”

group :test do
gem “rspec-rails”, “2.0.0.beta.18”
gem “factory_girl_rails”, “1.0”
end

My spec_helper :
http://github.com/chatgris/blabbr/blob/spec/spec/spec_helper.rb

First issue is related to stubbing the current_user method in
controller.

describe ‘current_user == added_by’ do

before :all do
  @current_user = Factory.create(:user)
  @smiley = Factory.create(:smiley)
end

before :each do
  controller.stub!(:logged_in?).and_return(true)
  controller.stub!(:current_user).and_return(@current_user)
end

it 'should be able to see index' do
  get :index
  response.should be_success
end

end

current_user is defined in application_controller.rb

That code have this result :

SmiliesController current_user == added_by should be able to see index
Failure/Error: controller.stub!(:logged_in?).and_return(true)
undefined method `stub!’ for #SmiliesController:0xb5e848b4
# ./spec/controllers/smilies_controller_spec.rb:36

The other issue is related to helpers specs. Considers this code :

require ‘spec_helper’

describe LinkHelper do
before :all do
@user = Factory.create(:creator)
@topic = Factory.build(:topic)
end

it “displays a 80px width gravatar link to the user page” do
helper.link_to_avatar(@user).should == “<a href="/users/creator
"><img alt="4f64c9f81bb0d4ee969aaf7b4a5a6f40" src="http://
www.gravatar.com/avatar/4f64c9f81bb0d4ee969aaf7b4a5a6f40.jpg?size=80"
/>”
end

end

That gives this error :

LinkHelper displays a 80px width gravatar link to the user page
Failure/Error: Unable to find matching line from backtrace
undefined local variable or method example' for #<RSpec::Core::ExampleGroup::Nested_8:0xb5fc4bac> # /home/chatgris/.bundle/ruby/1.8/bundler/gems/rspec- rails-863ffee12bd6030ece577b07c39297c14aacad85-master/lib/rspec/rails/ example/helper_example_group.rb:54:in _controller_path’
# /home/chatgris/.bundle/ruby/1.8/bundler/gems/rspec-
rails-863ffee12bd6030ece577b07c39297c14aacad85-master/lib/rspec/rails/
example/helper_example_group.rb:61

Thanks for your help.

On Jul 24, 2010, at 9:33 AM, chatgris wrote:

end

My spec_helper : http://github.com/chatgris/blabbr/blob/spec/spec/spec_helper.rb

You’ve got some unnecessary stuff in spec_helper.rb. Try this:

  1. Include rspec-rails in the :development group so you can see its rake
    tasks and generators [1]
  2. Move all the configuration (except mock_with :rspec) from spec_helper
    to another file in spec/support/.
  3. Regenerate spec_helper by running “script/rails generate
    rspec:install”

[1]
http://blog.davidchelimsky.net/2010/07/11/rspec-rails-2-generators-and-rake-tasks-part-ii/

First issue is related to stubbing the current_user method in
controller.

describe ‘current_user == added_by’ do

before :all do
@current_user = Factory.create(:user)
@smiley = Factory.create(:smiley)
end

This is probably not related to these issues, but it’s generally better
to create DB-backed objects (i.e. using Factory.create) in before(:each)
blocks so they get the benefit of Rails’ transaction management.

current_user is defined in application_controller.rb

That code have this result :

SmiliesController current_user == added_by should be able to see index
Failure/Error: controller.stub!(:logged_in?).and_return(true)
undefined method `stub!’ for #SmiliesController:0xb5e848b4

./spec/controllers/smilies_controller_spec.rb:36

This suggests that something is getting in the way of loading
rspec-mocks correctly. Please follow directions above and let me know if
that solves it.

it “displays a 80px width gravatar link to the user page” do
LinkHelper displays a 80px width gravatar link to the user page
Failure/Error: Unable to find matching line from backtrace
undefined local variable or method `example’ for
#RSpec::Core::ExampleGroup::Nested_8:0xb5fc4bac

/home/chatgris/.bundle/ruby/1.8/bundler/gems/rspec-

rails-863ffee12bd6030ece577b07c39297c14aacad85-master/lib/rspec/rails/
example/helper_example_group.rb:54:in `_controller_path’

I think this is the source of the problem. It looks like bundler is
loading some version of rspec-rails other than the “2.0.0.beta.18” that
is specified in the Gemfile.

What version of bundler are you using?

El 24/07/2010, a las 16:33, chatgris
escribió:

I have two issues with beta 2.0.0.beta.18. First, here’s the gems :

gem “rails”, “3.0.0.beta4”
gem “mongoid”, “2.0.0.beta10”

group :test do
gem “rspec-rails”, “2.0.0.beta.18”
gem “factory_girl_rails”, “1.0”
end

[snip]

rails-863ffee12bd6030ece577b07c39297c14aacad85-master/lib/rspec/rails/
example/helper_example_group.rb:61

Strange that you’ve asked for 2.0.0.beta.18 and Bundler is giving you a
Git version. What version of Bundler are you using?

Given that “running_example” was renamed to “example” in commit
f3de9311, I wonder if you have some kind of Bundler issue which means
you’re running a newer version of rspec-rails against an older version
of rspec-core.

Cheers,
Wincent

Thank you for those advices David.

It wasn’t related to bundler.

I did a big clean up in spec_helper.rb and in my gem list. I still had
rspec.rake in lib/tasks/ .

Now, everything works as expected via autotest or rake:spec.

Thanks.

Julien