[rspec-users] My very fist spec file (not sure what's wrong..)

Chris Sund chris at silhouettesolutions.net
Mon Jul 20 23:05:15 EDT 2009


Hey Everyone,

I am finally to the point in the rspec book where it describes how to
implement controller specs, so I thought what the heck, I'll give it a
try. I have an app I have been working on (Inside out) that I needed
to get test specs written for before I continue on with the app. Now
that I have read enough of the book I realize I was doing things a
little backwards in the "rspec" sense of BDD, but I thought I would
try to apply what I have learned so far to my existing code.

Here's the feedback I'm getting when I run my spec file....

NoMethodError in 'AccountsController POST create should build a new
account'
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.save
/Users/Chris/Rails/obilling/app/controllers/accounts_controller.rb:
24:in `create'
./spec/controllers/accounts_controller_spec.rb:7:
script/spec:10:


Here's my spec file...

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper' )

describe AccountsController, "POST create" do

it "should build a new account"  do
  Account.should_receive(:new).with("address" => "1201 washington
street")
  post :create, :account => {"address" => "1201 washington street"}
end


Here's the create method in my accounts_controller...

def create
    @account = Account.new(params[:account])
     if @account.save
      flash[:notice] = 'Account Record Saved'

    redirect_to(:action=>'edit', :id => @account)
    else
      flash[:warning] = 'Account Record Did Not Save!'
           render :action => 'new'

    end
  end


Keep in mind I'm a noob to everything (except programming). I have
been reading my brains out trying to get a better grasp on the ruby
syntax and how to apply this to rspec and rails. If someone has a
moment to explain this error. I understand that the @account instance
variable is "nil" in the error, but I don't understand why. I'm sure
it has something to do with -- post :create, :account => {"address" =>
"1201 washington street"}. It must be a syntax problem, or the way I'm
defining :account.

Thanks!

Chris


More information about the rspec-users mailing list