I’m sorry, but this is beginning to feel like kicking myself in the head. I’m completely baffled by RSpec. Have watched video after video, read tutorial after tutorial, and still I’m just stuck on square one.
=== here is what I’m working with
http://github.com/fudgestudios/bort/tree/master
=== Errors
F 1) NoMethodError in 'bidding on an item should work' You have a nil object when you didn't expect it! You might have expected an instance of ActiveRecord::Base. The error occurred while evaluating nil.new_record? spec/controllers/auction_controller_spec.rb:16: spec/controllers/auction_controller_spec.rb:6: Finished in 0.067139 seconds 1 example, 1 failure
=== here is my controller action
def bid @bid = Bid.new(params[:bid]) @bid.save end
=== here is my test
require File.dirname(__FILE__) + '/../spec_helper' include ApplicationHelper include UsersHelper include AuthenticatedTestHelper describe 'bidding on an item' do controller_name :items before(:each) do @user = mock_user stub!(:current_user).and_return(@user) end it 'should work' do post 'bid', :bid => { :auction_id => 1, :user_id => @user.id, :point => 1 } assigns[:bid].should be_new_record end end
=== spec_helper
http://github.com/fudgestudios/bort/tree/master/spec/spec_helper.rb
It’s very disheartening to wake for work at 3 a.m. and accomplish nothing for the day. Please understand.
You’ve got a couple of things backwards in before(:each). Seeing as the example is specifying that the post should increase the count by 1, you’re dealing with real records and there is no reason for stubbing anything at all. Also, at this point, since there is only one example, there is no reason to have a before block. I’d do it this way:
One thing I’d recommend is creating these things VERY granularly for now until you understand them better. Start with the expectation (post should change bid count), run the spec and let the failure message guide you to add whatever else you need in the spec or in the code.