[rspec-users] Controller stub and helper spec with lastest beta

David Chelimsky dchelimsky at gmail.com
Sat Jul 24 11:24:57 EDT 2010


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

> 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

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/.
2. 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.

>    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

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.

> 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\"
> /></a>"
>  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'

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?


>    # /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.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list