[rspec-users] Unexpected message on :attr_accessor

aslak hellesoy aslak.hellesoy at gmail.com
Tue Dec 4 14:27:39 EST 2007


On Dec 4, 2007 8:02 PM, David Chelimsky <dchelimsky at gmail.com> wrote:
> On Dec 4, 2007 11:49 AM, schleg <schleg at gmail.com> wrote:
> >
> > This may be a dumb noob issue, but I haven't found any answers while seaching
> > the forum--
> >
> > I have a controller method
> >
> >   def edit
> >     @user = User.find params[:id]
> >     @user.password_confirmation = @user.password
> >   end
> >
> > The User class has an "attr_accessor :password_confirmation" definition (so
> > "password_confirmation" doesn't exist in the users table). My spec has the
> > following
> >
> >   it "should find User on GET to users/edit/:id" do
> >     User.should_receive(:find).and_return(@user)
> >     @user.should_receive(:password_confirmation)
>
> This should be:
>
> @user.should_receive(:password_confirmation=)
>

Lots of beginners make this mistake. Maybe RSpec's mock framework
should be smart enough to suggest this fix by itself.

Patch anyone?

Aslak

> Cheers,
> David
>
>
> >     get 'edit', :id => @user.id
> >   end
> >
> > I am asking it to expect that I will be assigning something to that
> > attribute in the "edit" method. Unfortunately, the spec fails with
> >
> > Spec::Mocks::MockExpectationError in 'UsersController should find User on
> > GET to users/edit/:id'
> > Mock 'user' received unexpected message :password_confirmation= with
> > ("password")
> >
> > Initially I thought that my "should_receive" expectation was incorrectly
> > written, but if I comment out the attribute assignment in the controller
> > method
> >
> > ...
> > @user = User.find params[:id]
> > #@user.password_confirmation = @user.password
> > ...
> >
> > then the test fails with
> >
> > Spec::Mocks::MockExpectationError in 'UsersController should find User on
> > GET to users/edit/:id'
> > Mock 'user' expected :password_confirmation with (any args) once, but
> > received it 0 times
> >
> > So, it seems to me that the expectation is written correctly, but something
> > about using the attr_accessor via the mock object is causing a failure.
> >
> > BTW, I am doing the following in a before(:each) block
> >
> >     @user = mock("user")
> >     @user.stub!(:new_record?).and_return(false)
> >     @user.stub!(:update_attributes).and_return(true)
> >     @user.stub!(:password_confirmation).and_return('password')
> >     @user.stub!(:password).and_return('password')
> >     User.stub!(:new).and_return(@user)
> >
> > Does anyone know what I'm missing here?
> >
> > Thanks!
> > --
> > View this message in context: http://www.nabble.com/Unexpected-message-on-%3Aattr_accessor-tf4944588.html#a14155614
> > Sent from the rspec-users mailing list archive at Nabble.com.
> >
> > _______________________________________________
> > rspec-users mailing list
> > rspec-users at rubyforge.org
> > http://rubyforge.org/mailman/listinfo/rspec-users
> >
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list