I’m trying to wrap my brain around bdd, and it’s not going nearly as
well as I’d like. I’ve been struggling with it for a few weeks now, and
had been doing relatively well at first, while I was doing models. Now
I’m trying to understand how to spec controllers, and it’s got me a bit
befuddled. I’ve been reading what I can find on the internet and have
also watched the PeepCode screencasts. I have come a bit further in the
past few days, but I’ve hit a small snag with named routes that I can’t
quite figure out. To make matters worse, I’m implementing REST for the
first time as well. So I’m trying to get a grip on BDD and REST at the
same time.
Here’s what I have (applicable code only):
in the spec:
require File.dirname(FILE) + ‘/…/model_helper’
describe WelcomeController do
include ModelHelperMethods # for valid_user_hash
it ‘POST :log_in should log user in with correct email/password’ do
email = valid_user_hash[:email]
password = valid_user_hash[:password]
user = mock_model(User)
user.stub!(:id).and_return(1)
User.should_receive(:authenticate).with(email,
password).and_return(user)
post :log_in, {:email => email, :password => password} # this is
line 86 in the error
session[:user_id].should == user.id
response.should be_redirect
end
end
in the controller:
class WelcomeController < ApplicationController
def log_in
user = User.authenticate(params[:email], params[:password])
if user
session[:user_id] = user.id
redirect_to users_path(user) # this is line 15 in the error
else
flash[:error] = ‘Invalid login.’
redirect_to :action => :index
end
end
end
in routes.rb
map.resource :users
When I run the spec, I’m getting the error
NoMethodError in ‘WelcomeController POST :log_in should log user in with
correct email/password’
You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.to_sym
(eval):17:in users_path' /Users/phillip/dev/projects/cashflow/app/controllers/welcome_controller.rb:15:in
log_in’
./spec/controllers/001_welcome_controller_spec.rb:86:
script/spec:4:
I believe it has something to do with the call to users_path and it
trying to figure out the id of the object. That’s why I have the
user.stub!(:id) line in there. If I change
users_path(user)
to
users_path
in the controller, the error goes away. But since I’ve done a singleton
map.resource, I’m redirecting to the show method of the users
controller, and that requires an id, or so I believe.
Two questions, one specific and one more general:
- What’s the cause of the error and how can I resolve it? [Okay, that
was two questions there] - Am I one the right track as far as bdd-ing contollers?
Peace,
Phillip