Not sure why this is failing

I am not sure why the tests don’t see the call of the new method for the
Address class. It can be seen in the controller method where the
Address.new is called.

@address = Address.new(params[:address])

What am I doing wrong?

Thanks for the help.

Here is the error message:
Spec::Mocks::MockExpectationError in ‘UsersController handling POST
/users should create a new user’
Mock ‘Class’ expected :new with ({}) once, but received it 0 times
./spec/controllers/users_controller_spec.rb:245:
script/spec:4:

###################################

describe UsersController, “handling POST /users” do

before do
@address = mock_model(Address, :to_param => “1”)
@user = mock_model(User, :to_param => “1”, :address => @address,
:address= => nil)
User.stub!(:new).and_return(@user)
Address.stub!(:new).and_return(@address)
end

def post_with_successful_save
@user.should_receive(:save).and_return(true)
post :create, :user => {}
end

it “should create a new user” do
User.should_receive(:new).with({}).and_return(@user)
Address.should_receive(:new).with({}).and_return(@address) <–
Line 245
post_with_successful_save
end

end

###################################

class UsersController < ApplicationController

def create
@user = User.new(params[:user])
@address = Address.new(params[:address])
@user.address = @address

respond_to do |format|
  if @user.save
    flash[:notice] = 'User was successfully created.'
    format.html { redirect_to user_url(@user) }
    format.xml  { head :created, :location => user_url(@user) }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @user.errors.to_xml }
  end
end

end
end

On Nov 17, 2007, at 10:52 PM, Chris O. wrote:

I am not sure why the tests don’t see the call of the new method
for the
Address class. It can be seen in the controller method where the
Address.new is called.

@address = Address.new(params[:address])

What am I doing wrong?

You probably want

Address.should_receive(:new).with(no_args).and_return @address

Generally, if you have these sorts of mock expectation errors you can
write a more simple test by removing the “with” clause to see if the
is the message is being received with any arguments.

Scott

Address.should_receive(:new).with(no_args).and_return @address

That doesn’t seem to work either. I had also tried with :any and that
failed as well.

It does seem to work when removing the .with() call though.

Try with(nil)

I think params[:user] will return nil.

Steve R. wrote:

Try with(nil)

I think params[:user] will return nil.

yip that works, after fooling with it I also found that :any_args works
as well as any (I guess this one is more than just a symbol)

Thanks for the help.

personally I find this to be one of the most frustrating things about
rspec, well, rspec’s stubs
and wish the error messages could be much more helpful,
rather than just telling that an expected method was not called, tell
me if/when it was called with some other args instead
(if this causes excessive overhead, perhaps a runtime option like, –
trace “Address.new” )

–linoj

On Nov 18, 2007 10:10 AM, Jonathan L. [email protected]
wrote:

personally I find this to be one of the most frustrating things about
rspec, well, rspec’s stubs
and wish the error messages could be much more helpful,
rather than just telling that an expected method was not called, tell
me if/when it was called with some other args instead
(if this causes excessive overhead, perhaps a runtime option like, –
trace “Address.new” )

This sounds reasonable. Please submit a ticket to the tracker. A patch
would be best, but a bug report would be fine to get it on the radar.

Cheers,
David

OT I am so frustrated with Rubyforge. It refuses to let me log in. I
know I have an account as I can change my password, and go through
the login form, but it just returns with no cookie and not logged in
and no messages. Although I’m behind a proxy I do not have any
similar problems with any other sites, including ecommerce ones with
https. (osx, ff and safari) Perhaps its some lameness on my part, but
I’m at a loss how to further diagnose my problem. If anyone would
like to help me, I’ll be in #rubyforge as linoj. Nonetheless I am not
able to submit to tracker.

On Nov 18, 2007, at 12:04 AM, Chris O. wrote:

I think :any_args is deprecated, and you should use any_args()
instead (just FYI).

Glad you figured it out.

Scott

On Nov 18, 2007 11:15 AM, Jonathan L. [email protected]
wrote:

OT I am so frustrated with Rubyforge. It refuses to let me log in. I
know I have an account as I can change my password, and go through
the login form, but it just returns with no cookie and not logged in
and no messages. Although I’m behind a proxy I do not have any
similar problems with any other sites, including ecommerce ones with
https. (osx, ff and safari) Perhaps its some lameness on my part, but
I’m at a loss how to further diagnose my problem. If anyone would
like to help me, I’ll be in #rubyforge as linoj. Nonetheless I am not
able to submit to tracker.

Hey Jonathan,

You’ll be happy to know that we’ll soon be moving ticket tracking to
lighthouse.

In the mean time, I added
http://rubyforge.org/tracker/index.php?group_id=797&atid=3149.

Cheers,
David