is there a way to stub a method that sets an instance variable, so the stub sets it too? def find_foo @foo = Foo.find(params[:id] end ... controller.stub!(:find_foo).and_assigns(:foo, "123") -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071213/2042719f/attachment.html
On 13.12.2007, at 9.00, Jonathan Linowes wrote:> is there a way to stub a method that sets an instance variable, so > the stub sets it too? > > def find_foo > @foo = Foo.find(params[:id] > end > > > ... > controller.stub!(:find_foo).and_assigns(:foo, "123")Why don''t you just stub Foo.find? That way the instance var gets assigned automatically. //jarkko -- Jarkko Laine http://jlaine.net http://dotherightthing.com http://www.railsecommerce.com http://odesign.fi
On Dec 12, 2007 11:00 PM, Jonathan Linowes <jonathan at parkerhill.com> wrote:> > is there a way to stub a method that sets an instance variable, so the stub > sets it too? > > def find_foo > @foo = Foo.find(params[:id] > end > > > ... > controller.stub!(:find_foo).and_assigns(:foo, "123")huh? I think what you want to be doing is Foo.stub!(:find_foo).and_return @mock_foo and then assigns[:foo] will of course be set to @mock_foo. Pat
On Dec 13, 2007, at 2:00 AM, Jonathan Linowes wrote:> is there a way to stub a method that sets an instance variable, so > the stub sets it too? >Nope. The idea behind this is that instance variables are supposed to be the inner representation of some data inside a class, while an attr_accessor/reader is the public interface to other objects. Obviously, this idea breaks down in rails. If you wanted something like that, why not write some shared specs like this: it "should find ..." do do_action @class_name.constantize.should_receive(:find).with (params).and_return @instance_var_contents end it "should assign the instance variable to the template" do do_action assigns[@class_name.underscore].should == @instance_var_contents end where @class_name would be a string like "Foo" Scott> def find_foo > @foo = Foo.find(params[:id] > end > > > ... > controller.stub!(:find_foo).and_assigns(:foo, "123") > > > _______________________________________________ > 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/20071213/36c31800/attachment.html
On Dec 13, 2007, at 2:06 AM, Jarkko Laine wrote:> > On 13.12.2007, at 9.00, Jonathan Linowes wrote: > >> is there a way to stub a method that sets an instance variable, so >> the stub sets it too? >> >> def find_foo >> @foo = Foo.find(params[:id] >> end >> >> >> ... >> controller.stub!(:find_foo).and_assigns(:foo, "123") > > Why don''t you just stub Foo.find? That way the instance var gets > assigned automatically. > > //jarkko >Sorry, perhaps I gave too simplistic an example. My question is what if I want to stub the whole find_foo method but one of its side effects is it sets @foo (rather than stub the internals of find_foo)
On Dec 13, 2007, at 2:47 AM, Jonathan Linowes wrote:> > On Dec 13, 2007, at 2:06 AM, Jarkko Laine wrote: > >> >> On 13.12.2007, at 9.00, Jonathan Linowes wrote: >> >>> is there a way to stub a method that sets an instance variable, so >>> the stub sets it too? >>> >>> def find_foo >>> @foo = Foo.find(params[:id] >>> end >>> >>> >>> ... >>> controller.stub!(:find_foo).and_assigns(:foo, "123") >> >> Why don''t you just stub Foo.find? That way the instance var gets >> assigned automatically. >> >> //jarkko >> > > Sorry, perhaps I gave too simplistic an example. > My question is what if I want to stub the whole find_foo method > but one of its side effects is it sets @foo > (rather than stub the internals of find_foo) > >... and @foo is used by the method that i am testing
I wouldn''t try doing this. You risk getting highly coupled test code. I''d rather use a little helper method that does this for you. Stefan 2007/12/13, Jonathan Linowes <jonathan at parkerhill.com>:> > > On Dec 13, 2007, at 2:47 AM, Jonathan Linowes wrote: > > > > > On Dec 13, 2007, at 2:06 AM, Jarkko Laine wrote: > > > >> > >> On 13.12.2007, at 9.00, Jonathan Linowes wrote: > >> > >>> is there a way to stub a method that sets an instance variable, so > >>> the stub sets it too? > >>> > >>> def find_foo > >>> @foo = Foo.find(params[:id] > >>> end > >>> > >>> > >>> ... > >>> controller.stub!(:find_foo).and_assigns(:foo, "123") > >> > >> Why don''t you just stub Foo.find? That way the instance var gets > >> assigned automatically. > >> > >> //jarkko > >> > > > > Sorry, perhaps I gave too simplistic an example. > > My question is what if I want to stub the whole find_foo method > > but one of its side effects is it sets @foo > > (rather than stub the internals of find_foo) > > > > > ... and @foo is used by the method that i am testing > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-- Bekk Open Source http://boss.bekk.no -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071213/06c5d38e/attachment.html
>> Sorry, perhaps I gave too simplistic an example. >>Real code, please? :-) Simplified examples only ever result in simplistic suggestions. Daniel On 13 Dec 2007, at 07:55 13 Dec 2007, Jonathan Linowes wrote:> > On Dec 13, 2007, at 2:47 AM, Jonathan Linowes wrote: > >> >> On Dec 13, 2007, at 2:06 AM, Jarkko Laine wrote: >> >>> >>> On 13.12.2007, at 9.00, Jonathan Linowes wrote: >>> >>>> is there a way to stub a method that sets an instance variable, so >>>> the stub sets it too? >>>> >>>> def find_foo >>>> @foo = Foo.find(params[:id] >>>> end >>>> >>>> >>>> ... >>>> controller.stub!(:find_foo).and_assigns(:foo, "123") >>> >>> Why don''t you just stub Foo.find? That way the instance var gets >>> assigned automatically. >>> >>> //jarkko >>> >> >> Sorry, perhaps I gave too simplistic an example. >> My question is what if I want to stub the whole find_foo method >> but one of its side effects is it sets @foo >> (rather than stub the internals of find_foo) >> >> > ... and @foo is used by the method that i am testing > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
thx I ended up wrapping the instance variable in a method, which I can then stub also Its cleaner that way anyhow def current_foo @foo end On Dec 13, 2007, at 9:06 AM, Daniel Tenner wrote:>>> Sorry, perhaps I gave too simplistic an example. >>> > Real code, please? :-) > > Simplified examples only ever result in simplistic suggestions. > > Daniel > > On 13 Dec 2007, at 07:55 13 Dec 2007, Jonathan Linowes wrote: > >> >> On Dec 13, 2007, at 2:47 AM, Jonathan Linowes wrote: >> >>> >>> On Dec 13, 2007, at 2:06 AM, Jarkko Laine wrote: >>> >>>> >>>> On 13.12.2007, at 9.00, Jonathan Linowes wrote: >>>> >>>>> is there a way to stub a method that sets an instance variable, so >>>>> the stub sets it too? >>>>> >>>>> def find_foo >>>>> @foo = Foo.find(params[:id] >>>>> end >>>>> >>>>> >>>>> ... >>>>> controller.stub!(:find_foo).and_assigns(:foo, "123") >>>> >>>> Why don''t you just stub Foo.find? That way the instance var gets >>>> assigned automatically. >>>> >>>> //jarkko >>>> >>> >>> Sorry, perhaps I gave too simplistic an example. >>> My question is what if I want to stub the whole find_foo method >>> but one of its side effects is it sets @foo >>> (rather than stub the internals of find_foo) >>> >>> >> ... and @foo is used by the method that i am testing >> >> _______________________________________________ >> 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