Marcelo de Moraes Serpa
2010-Jun-07 21:43 UTC
[rspec-users] How to test the assignment of current_user
Hello, This could be rephrased as -- How to test calls of special assigment (setter) methods? I am trying to test the assigmnet of a user object to the current_user=(new_user) method. It is part of an authentication process and finally the object is assigned to the session through this method. I have tried: controller.should_receive(:current_user=).with(@user) post :create, @params And the output is: expected :current_user= with (#<User id: 605 <.... snip ...> ) once, but received it 0 times I do not understand, since current_user=(new_user) is actually being called by the controller: def create <snip> current_user = @user <snip> end Why''s that? Thanks, Marcelo. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100607/aac452b3/attachment.html>
VinÃcius Baggio Fuentes
2010-Jun-07 22:48 UTC
[rspec-users] How to test the assignment of current_user
If current_user is an instance variable (as if you did attr_accessor :current_user), you should try: @current_user = @user If not, you should try invoking with self: self.current_user = @user because ruby is creating a local variable named current_user instead of invoking #current_user=. When you do call self, which refers to the object itself, it will use normal method lookup, that will find your #current_user=. On Mon, Jun 7, 2010 at 6:43 PM, Marcelo de Moraes Serpa <celoserpa at gmail.com> wrote:> Hello, > > This could be rephrased as -- How to test calls of special assigment > (setter) methods? > > I am trying to test the assigmnet of a user object to the > current_user=(new_user) method. It is part of an authentication process and > finally the object is assigned to the session through this method. I have > tried: > > controller.should_receive(:current_user=).with(@user) > post :create, @params > > And the output is: > > expected :current_user= with (#<User id: 605 <.... snip ...> ) once, but > received it 0 times > > I do not understand, since current_user=(new_user) is actually being called > by the controller: > > def create > <snip> > current_user = @user > <snip> > end > > Why''s that? > > Thanks, > > Marcelo. > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100607/f0e91c92/attachment.html>
VinÃcius Baggio Fuentes
2010-Jun-07 22:50 UTC
[rspec-users] How to test the assignment of current_user
Oh yes, forgot to add that, if you do self.current_user = user, it will call your mock as expected. Sorry for the multitude of emails. 2010/6/7 Vin?cius Baggio Fuentes <vinibaggio at gmail.com>> If current_user is an instance variable (as if you did attr_accessor > :current_user), you should try: > @current_user = @user > > If not, you should try invoking with self: > self.current_user = @user > > because ruby is creating a local variable named current_user instead of > invoking #current_user=. When you do call self, which refers to the object > itself, it will use normal method lookup, that will find your > #current_user=. > > On Mon, Jun 7, 2010 at 6:43 PM, Marcelo de Moraes Serpa < > celoserpa at gmail.com> wrote: > >> Hello, >> >> This could be rephrased as -- How to test calls of special assigment >> (setter) methods? >> >> I am trying to test the assigmnet of a user object to the >> current_user=(new_user) method. It is part of an authentication process and >> finally the object is assigned to the session through this method. I have >> tried: >> >> controller.should_receive(:current_user=).with(@user) >> post :create, @params >> >> And the output is: >> >> expected :current_user= with (#<User id: 605 <.... snip ...> ) once, but >> received it 0 times >> >> I do not understand, since current_user=(new_user) is actually being >> called by the controller: >> >> def create >> <snip> >> current_user = @user >> <snip> >> end >> >> Why''s that? >> >> Thanks, >> >> Marcelo. >> >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100607/700f5e13/attachment-0001.html>
Marcelo de Moraes Serpa
2010-Jun-08 16:44 UTC
[rspec-users] How to test the assignment of current_user
Thanks Vin?cius ;) 2010/6/7 Vin?cius Baggio Fuentes <vinibaggio at gmail.com>> Oh yes, forgot to add that, if you do self.current_user = user, it will > call your mock as expected. > > Sorry for the multitude of emails. > > > 2010/6/7 Vin?cius Baggio Fuentes <vinibaggio at gmail.com> > > If current_user is an instance variable (as if you did attr_accessor >> :current_user), you should try: >> @current_user = @user >> >> If not, you should try invoking with self: >> self.current_user = @user >> >> because ruby is creating a local variable named current_user instead of >> invoking #current_user=. When you do call self, which refers to the object >> itself, it will use normal method lookup, that will find your >> #current_user=. >> >> On Mon, Jun 7, 2010 at 6:43 PM, Marcelo de Moraes Serpa < >> celoserpa at gmail.com> wrote: >> >>> Hello, >>> >>> This could be rephrased as -- How to test calls of special assigment >>> (setter) methods? >>> >>> I am trying to test the assigmnet of a user object to the >>> current_user=(new_user) method. It is part of an authentication process and >>> finally the object is assigned to the session through this method. I have >>> tried: >>> >>> controller.should_receive(:current_user=).with(@user) >>> post :create, @params >>> >>> And the output is: >>> >>> expected :current_user= with (#<User id: 605 <.... snip ...> ) once, but >>> received it 0 times >>> >>> I do not understand, since current_user=(new_user) is actually being >>> called by the controller: >>> >>> def create >>> <snip> >>> current_user = @user >>> <snip> >>> end >>> >>> Why''s that? >>> >>> Thanks, >>> >>> Marcelo. >>> >>> >>> _______________________________________________ >>> 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 >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100608/3fdefb21/attachment.html>