newbie
2008-Apr-06 19:17 UTC
[rspec-users] how to stub the :current_user and any methods associated with it
I''m new to Rspec and I''m having a bit of trouble with this controller that I''m testing. I think I''m doing it right but I guess my syntax is wrong. I''m currently using the "acts_as_authenticated" plug in. What I want to test out is EventController on entering the tickets_page - should show the tickets_page if the current_user has NOT entered this page today ----------------------------------------------------- Below is my event controller ----------------------------------------------------- class EventController < ApplicationController before_filter :login_required def tickets_page if current_user.has_already_entered_today? flash[:notice] = ''Come back tomorrow'' redirect_to :action => ''home'' else flash[:notice] = ''Welcome'' end end end ----------------------------------------------------- Below is my rspec for this controller ----------------------------------------------------- require File.dirname(__FILE__) + ''/../spec_helper'' describe EventController do before(:each) do @current_user = mock_model(User, :id => 1) controller.stub!(:current_user).and_return(@current_user) controller.stub!(:login_required).and_return(:true) end fixtures :users describe "on entering the tickets page" do it "should show the tickets_page if the current_user has NOT entered this page today" do controller.current_user.stub! (:has_already_entered_today?).and_return(:false) get :tickets_page controller.current_user.should_receive(:has_already_entered_today?).with(:false).and_return(:false) response.should render_template(:tickets_page) end end end ----------------------------------------------------- My errors ----------------------------------------------------- Mock ''User_1001'' received unexpected message :has_already_entered_today? with (no args) Anyone can help with any direction of what I might be doing wrong?
Bryan Ray
2008-Apr-07 15:31 UTC
[rspec-users] how to stub the :current_user and any methods associated with it
Responses shown below... Sorry this isn''t extremely informative ... in a bit of a rush. Hopefully it will point you in the appropriate direction. On Sun, Apr 6, 2008 at 2:17 PM, newbie <alen198 at gmail.com> wrote:> I''m new to Rspec and I''m having a bit of trouble with this controller > that I''m testing. I think I''m doing it right but I guess my syntax is > wrong. > I''m currently using the "acts_as_authenticated" plug in. > > What I want to test out is > > EventController on entering the tickets_page > - should show the tickets_page if the current_user has NOT entered > this page today > > > ----------------------------------------------------- > Below is my event controller > ----------------------------------------------------- > > class EventController < ApplicationController > > before_filter :login_required > > def tickets_page > if current_user.has_already_entered_today? > flash[:notice] = ''Come back tomorrow'' > redirect_to :action => ''home'' > else > flash[:notice] = ''Welcome'' > end > end > end > > > ----------------------------------------------------- > Below is my rspec for this controller > ----------------------------------------------------- > > require File.dirname(__FILE__) + ''/../spec_helper'' > describe EventController do > > before(:each) do > @current_user = mock_model(User, :id => 1) > controller.stub!(:current_user).and_return(@current_user) > controller.stub!(:login_required).and_return(:true) > end > > fixtures :users > > > describe "on entering the tickets page" do > it "should show the tickets_page if the current_user has NOT > entered this page today" do > controller.current_user.stub! > (:has_already_entered_today?).and_return(:false) > get :tickets_page > > > controller.current_user.should_receive(:has_already_entered_today?).with(:false).and_return(:false) > response.should render_template(:tickets_page) > end > end > > end > > ----------------------------------------------------- > My errors > ----------------------------------------------------- > Mock ''User_1001'' received unexpected > message :has_already_entered_today? with (no args)So this is a good start ... it means that the controller action is actually being mocked out ... you just need to keep moving forward mocking/stubbing out the rest of your controller actions. it''s complaining that it doesn''t know what to do on your *current_user.has_already_entered_today?* method. @current_user.stub!(:has_already_entered_today?).and_return(<insert true or false here>)> > > > Anyone can help with any direction of what I might be doing wrong? > > > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-- Bryan Ray http://www.bryanray.net "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20080407/3fc1fc06/attachment.html
newbie
2008-Apr-11 01:57 UTC
[rspec-users] how to stub the :current_user and any methods associated with it
Thanks for the direction. I''m glad to hear that I''m going in the right direction. I just have a final question and I seem to be hitting a stub/mocking road block at the moment. In my controller I have this... def index @entries = current_user.entries end How would I rspec this out correctly? This is what I have but it looks wrong. before(:each) do @current_user = mock_model(User, :id => 1) @entry = mock_model(Entry, :user_id => 1) controller.stub!(:current_user).and_return(@current_user) controller.stub!(:entries).and_return(@entry) end describe "on the index page" do it "should show all of the current_users entries" do @current_user.should_receive(:entries).with(@entry).and_return(true) end end On Apr 7, 11:31?am, "Bryan Ray" <bryans... at gmail.com> wrote:> Responses shown below... > > Sorry this isn''t extremely informative ... in a bit of a rush. Hopefully it > will point you in the appropriate direction. > > > > On Sun, Apr 6, 2008 at 2:17 PM, newbie <alen... at gmail.com> wrote: > > I''m new to Rspec and I''m having a bit of trouble with this controller > > that I''m testing. I think I''m doing it right but I guess my syntax is > > wrong. > > I''m currently using the "acts_as_authenticated" plug in. > > > What I want to test out is > > > EventController on entering the tickets_page > > - should show the tickets_page ?if the current_user has NOT entered > > this page today > > > ----------------------------------------------------- > > Below is my event controller > > ----------------------------------------------------- > > > class EventController < ApplicationController > > > ?before_filter :login_required > > > ?def tickets_page > > ? ? ?if current_user.has_already_entered_today? > > ? ? ? ?flash[:notice] = ''Come back tomorrow'' > > ? ? ? ?redirect_to :action => ''home'' > > ? ? ?else > > ? ? ? ?flash[:notice] = ''Welcome'' > > ? ? ?end > > ?end > > end > > > ----------------------------------------------------- > > Below is my rspec for this controller > > ----------------------------------------------------- > > > require File.dirname(__FILE__) + ''/../spec_helper'' > > describe EventController do > > > ?before(:each) do > > ? ?@current_user = mock_model(User, :id => 1) > > ? ?controller.stub!(:current_user).and_return(@current_user) > > ? ?controller.stub!(:login_required).and_return(:true) > > ?end > > > ?fixtures :users > > > ?describe "on entering the tickets page" do > > ? ?it "should show the tickets_page ?if the current_user has NOT > > entered this page today" do > > ? ? controller.current_user.stub! > > (:has_already_entered_today?).and_return(:false) > > ? ? get :tickets_page > > > controller.current_user.should_receive(:has_already_entered_today?).with(:f alse).and_return(:false) > > ? ? response.should render_template(:tickets_page) > > ? ?end > > end > > > end > > > ----------------------------------------------------- > > My errors > > ----------------------------------------------------- > > Mock ''User_1001'' received unexpected > > message :has_already_entered_today? with (no args) > > So this is a good start ... it means that the controller action is actually > being mocked out ... you just need to keep moving forward mocking/stubbing > out the rest of your controller actions. it''s complaining that it doesn''t > know what to do on your *current_user.has_already_entered_today?* method. > > @current_user.stub!(:has_already_entered_today?).and_return(<insert true or > false here>) > > > > > Anyone can help with any direction of what I might be doing wrong? > > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.org > >http://rubyforge.org/mailman/listinfo/rspec-users > > -- > Bryan Rayhttp://www.bryanray.net > > "Programming today is a race between software engineers striving to build > bigger and better idiot-proof programs, and the Universe trying to produce > bigger and better idiots. So far, the Universe is winning." > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users
Jarkko Laine
2008-Apr-11 07:21 UTC
[rspec-users] how to stub the :current_user and any methods associated with it
On 11.4.2008, at 4.57, newbie wrote:> Thanks for the direction. I''m glad to hear that I''m going in the right > direction. I just have a final question and I seem to be hitting a > stub/mocking road block at the moment. In my controller I have this... > > def index > @entries = current_user.entries > end > > How would I rspec this out correctly? This is what I have but it looks > wrong. > > before(:each) do > @current_user = mock_model(User, :id => 1) > @entry = mock_model(Entry, :user_id => 1) > controller.stub!(:current_user).and_return(@current_user) > controller.stub!(:entries).and_return(@entry) > end > > > describe "on the index page" do > it "should show all of the current_users entries" do > > @current_user.should_receive(:entries).with(@entry).and_return(true)This is wrong. The with method means parameters the expected method call receives. In your case it gets no parameters. Besides, since you stub the entries method anyway, you can just specify the behaviour. What you probably want with the code above is this: assigns[:entries].should == @entry //jarkko> > end > end > > > > > > > > > > > On Apr 7, 11:31 am, "Bryan Ray" <bryans... at gmail.com> wrote: >> Responses shown below... >> >> Sorry this isn''t extremely informative ... in a bit of a rush. >> Hopefully it >> will point you in the appropriate direction. >> >> >> >> On Sun, Apr 6, 2008 at 2:17 PM, newbie <alen... at gmail.com> wrote: >>> I''m new to Rspec and I''m having a bit of trouble with this >>> controller >>> that I''m testing. I think I''m doing it right but I guess my syntax >>> is >>> wrong. >>> I''m currently using the "acts_as_authenticated" plug in. >> >>> What I want to test out is >> >>> EventController on entering the tickets_page >>> - should show the tickets_page if the current_user has NOT entered >>> this page today >> >>> ----------------------------------------------------- >>> Below is my event controller >>> ----------------------------------------------------- >> >>> class EventController < ApplicationController >> >>> before_filter :login_required >> >>> def tickets_page >>> if current_user.has_already_entered_today? >>> flash[:notice] = ''Come back tomorrow'' >>> redirect_to :action => ''home'' >>> else >>> flash[:notice] = ''Welcome'' >>> end >>> end >>> end >> >>> ----------------------------------------------------- >>> Below is my rspec for this controller >>> ----------------------------------------------------- >> >>> require File.dirname(__FILE__) + ''/../spec_helper'' >>> describe EventController do >> >>> before(:each) do >>> @current_user = mock_model(User, :id => 1) >>> controller.stub!(:current_user).and_return(@current_user) >>> controller.stub!(:login_required).and_return(:true) >>> end >> >>> fixtures :users >> >>> describe "on entering the tickets page" do >>> it "should show the tickets_page if the current_user has NOT >>> entered this page today" do >>> controller.current_user.stub! >>> (:has_already_entered_today?).and_return(:false) >>> get :tickets_page >> >>> controller >>> .current_user.should_receive(:has_already_entered_today?).with(:f >>> alse).and_return(:false) >>> response.should render_template(:tickets_page) >>> end >>> end >> >>> end >> >>> ----------------------------------------------------- >>> My errors >>> ----------------------------------------------------- >>> Mock ''User_1001'' received unexpected >>> message :has_already_entered_today? with (no args) >> >> So this is a good start ... it means that the controller action is >> actually >> being mocked out ... you just need to keep moving forward mocking/ >> stubbing >> out the rest of your controller actions. it''s complaining that it >> doesn''t >> know what to do on your *current_user.has_already_entered_today?* >> method. >> >> @current_user.stub!(:has_already_entered_today?).and_return(<insert >> true or >> false here>) >> >> >> >>> Anyone can help with any direction of what I might be doing wrong? >> >>> _______________________________________________ >>> rspec-users mailing list >>> rspec-us... at rubyforge.org >>> http://rubyforge.org/mailman/listinfo/rspec-users >> >> -- >> Bryan Rayhttp://www.bryanray.net >> >> "Programming today is a race between software engineers striving to >> build >> bigger and better idiot-proof programs, and the Universe trying to >> produce >> bigger and better idiots. So far, the Universe is winning." >> >> _______________________________________________ >> rspec-users mailing list >> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users-- Jarkko Laine http://jlaine.net http://dotherightthing.com http://www.railsecommerce.com http://odesign.fi -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2417 bytes Desc: not available Url : http://rubyforge.org/pipermail/rspec-users/attachments/20080411/1b9e1afb/attachment-0001.bin
newbie
2008-Apr-12 19:24 UTC
[rspec-users] how to stub the :current_user and any methods associated with it
Hey Jarkko, Thanks for the feedback. I tried your suggestion and it doesn''t seem to work. To quickly recap I''m just testing in my controller that this works def index @entries = current_user.entries end describe "on the main page" do it "should show all the current entries for the user" do get :index assigns[:entries].should == @entries end
Jarkko Laine
2008-Apr-13 17:03 UTC
[rspec-users] how to stub the :current_user and any methods associated with it
On 12.4.2008, at 22.24, newbie wrote:> Hey Jarkko, > > Thanks for the feedback. I tried your suggestion and it doesn''t seem > to work.What doesn''t work?> > > To quickly recap I''m just testing in my controller that this works > > def index > @entries = current_user.entries > end > > describe "on the main page" do > it "should show all the current entries for the user" do > get :index > assigns[:entries].should == @entries > endI don''t see you logging in a user anywhere. //jarkko -- Jarkko Laine http://jlaine.net http://dotherightthing.com http://www.railsecommerce.com http://odesign.fi -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2417 bytes Desc: not available Url : http://rubyforge.org/pipermail/rspec-users/attachments/20080413/c5ab6e24/attachment.bin
Shane Mingins
2008-Apr-13 20:50 UTC
[rspec-users] how to stub the :current_user and any methods associated with it
Looking at yr earlier post ... controller.stub!(:entries).and_return(@entry) Not sure why you are stubbing entries on the controller ... entries is called on the current user ... I tend to do this ... @current_user = mock_model(User, :id => 1, :entries => [@entry]) HTH Shane On 13/04/2008, at 7:24 AM, newbie wrote:> Hey Jarkko, > > Thanks for the feedback. I tried your suggestion and it doesn''t seem > to work. > > To quickly recap I''m just testing in my controller that this works > > def index > @entries = current_user.entries > end > > describe "on the main page" do > it "should show all the current entries for the user" do > get :index > assigns[:entries].should == @entries > end > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
newbie
2008-Apr-15 23:08 UTC
[rspec-users] how to stub the :current_user and any methods associated with it
> Not sure why you are stubbing entries on the controller ... entries is > called on the current user ...I have this method. I want to use this variable to loop the users @entries. So I''m testing to see that this variable is tested and works correctly. def main @entries = current_user.entries end I tried your suggestion @current_user = mock_model(User, :id => 1, :entries => [@entry]) My error "Mock ''User_1010'' received unexpected message :entries with (no args)" Maybe this is overtesting or not really necessary to test. Let me know. ---------------------------------- I''m testing this out ---------------------------------- def main @entries = current_user.entries end ------------------------------------------ Using this spec ------------------------------------------ before(:each) do @current_user = mock_model(User, :id => 1) @entry = mock_model(Entry, :user_id => 1) controller.stub!(:current_user).and_return(@current_user) controller.stub!(:login_required).and_return(:true) controller.stub!(:entries).and_return(@entry) end describe "on the main page" do it "should show all the current entries for the user" do @current_user = mock_model(User, :id => 1, :entries => [@entry]) get :main end end
David Chelimsky
2008-Apr-15 23:18 UTC
[rspec-users] how to stub the :current_user and any methods associated with it
On Apr 15, 2008, at 7:08 PM, newbie wrote:>> Not sure why you are stubbing entries on the controller ... entries >> is >> called on the current user ... > > I have this method. I want to use this variable to loop the users > @entries. So I''m testing to see that this variable is tested and works > correctly. > > def main > @entries = current_user.entries > end > > I tried your suggestion > @current_user = mock_model(User, :id => 1, :entries => [@entry]) > > My error > "Mock ''User_1010'' received unexpected message :entries with (no args)" > > Maybe this is overtesting or not really necessary to test. Let me > know. > > ---------------------------------- > I''m testing this out > ---------------------------------- > def main > @entries = current_user.entries > end > > > ------------------------------------------ > Using this spec > ------------------------------------------ > > before(:each) do > @current_user = mock_model(User, :id => 1) > @entry = mock_model(Entry, :user_id => 1) > controller.stub!(:current_user).and_return(@current_user) > controller.stub!(:login_required).and_return(:true) > controller.stub!(:entries).and_return(@entry) > end > > describe "on the main page" do > it "should show all the current entries for the user" do > @current_user = mock_model(User, :id => 1, :entries => [@entry]) > get :main > end > end@current_user is being assigned values twice - once in before(:each) and once in describe "on the main page". The mock that is raising the error is the one defined in before(:each), which is the one that is NOT told to expect :entries, and is the one that is returned by controller.current_user (per the 3rd line of before(:each)). That all make sense? Not really a mock or rspec problem but really just understanding how assignments work in Ruby. HTH, David
Fernando Perez
2008-Nov-05 14:05 UTC
[rspec-users] how to stub the :current_user and any methods associated with it
I''m also having problems stubbing current_user. I cannot put controller.stub!(:current_user).and_return(@current_user) in a before(:all) block. The stubbing doesn''t happen. However if I put it in the it "should..." block, then it works. Why is that? -- Posted via http://www.ruby-forum.com/.
Fernando Perez
2008-Nov-05 14:06 UTC
[rspec-users] how to stub the :current_user and any methods associated with it
Actually, in a before(:each) block, the stubbing works. So why doesn''t it work with a before(:all)? -- Posted via http://www.ruby-forum.com/.
David Chelimsky
2008-Nov-05 14:12 UTC
[rspec-users] how to stub the :current_user and any methods associated with it
On Wed, Nov 5, 2008 at 8:06 AM, Fernando Perez <lists at ruby-forum.com> wrote:> Actually, in a before(:each) block, the stubbing works. So why doesn''t > it work with a before(:all)?Please, please, please quote the information from the previous email to which you are referring. I''ll respond to your first email.
David Chelimsky
2008-Nov-05 14:14 UTC
[rspec-users] how to stub the :current_user and any methods associated with it
On Wed, Nov 5, 2008 at 8:05 AM, Fernando Perez <lists at ruby-forum.com> wrote:> I''m also having problems stubbing current_user. > > I cannot put > controller.stub!(:current_user).and_return(@current_user) > in a before(:all) block. The stubbing doesn''t happen.Stubs and mocks get cleared out after each example. before(:all) only runs once, so after the first example in a group runs, the stub gets cleared out and does not get recreated. before(:each) runs before each example, setting the stub each time. So use before(:each) instead of before(:all). Make sense? There''s a little more info about this on http://rspec.info/documentation/before_and_after.html (near the bottom). Cheers, David> > However if I put it in the it "should..." block, then it works. > > Why is that? > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Fernando Perez
2008-Nov-05 14:22 UTC
[rspec-users] how to stub the :current_user and any methods associated with it
Thanks for the clarification. And from now on I will quote previous emails. -- Posted via http://www.ruby-forum.com/.
David Chelimsky
2008-Nov-05 14:37 UTC
[rspec-users] how to stub the :current_user and any methods associated with it
On Wed, Nov 5, 2008 at 8:22 AM, Fernando Perez <lists at ruby-forum.com> wrote:> Thanks for the clarification. And from now on I will quote previous > emails.Sorry man, but I''m laughing my ass off over this. Readers of this email will have no idea what I clarified for you because you *didn''t quote the clarification* :) :) :) We''ll keep working on this and get it right sooner or later. Cheers, David