views:

240

answers:

2

I have been attempting to dive into RSpec 2 but its auto generated controller specs do not work for any version of RSpec 2 with any version of Ruby or any version of Rails. Maybe I'm missing something obvious?

def mock_category(stubs={})
  @mock_category ||= mock_model(Category, stubs).as_null_object
end

describe "GET show" do
  it "assigns the requested category as @category" do
    Category.stub(:find).with("37") { mock_category }
    get :show, :id => "37"
    assigns(:category).should be(mock_category)
  end
end

This is auto generated from rails g scaffold Category

RSpec returns this :

Failures:
   1) CategoriesController GET show assigns the requested category as @category
    Failure/Error: assigns(:category).should be(mock_category)
    expected Category_1002, got nil
    # ./spec/controllers/categories_controller_spec.rb:21
    # /Library/Ruby/Gems/1.8/gems/activesupport-3.0.0/lib/active_support/dependencies.rb:239:in `inject'

Why is this mock/stub returning nil ?

Update

This is from my controller's show method :

def show
   @category = Category.find(params[:id])

  respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml => @category }
  end
end

Thanks!

A: 

Hmm. Well if something's wrong, I sure don't see it. Maybe the show action isn't being reached at all? Are there any before_filter statements getting in the way?

You could try adding some tests to see if the assignment is being reached. Like

controller.should_receive(:show)

btw .as_null_object tells the mock to ignore messages that you don't stub. This helps with objects that depend on validations or other constraints, which you would otherwise have to stub out in order to get an object that you can test. But be careful not to ignore messages that you should be testing for.

zetetic
I think I've deduced this down to that its missing some fundamental part of the generator. Because I can't recreate the problem , I find that because the project was started with Rails 3 beta, and the new Rspec, that the generators conflict. And I'm not sure how to strip rspec from the project and regenreate them.
Trip
You've heard the saying, "never trust a junkie"? To that I add: "never trust a generator" :)
zetetic
A: 

RSpec has some serious conflicts between Rails3 beta, and RSpec 2 beta.10 to Rails3 release, and RSpec 2 beta.20.

I tried copying and pasting the differences between the scaffolds, but I cleared up the sitation entirely by deleting all the specs, and regenerating them. Uninstalling haml, and installing only haml-rails for rails 3.

All the specs run now.

Trip