[rspec-users] stub with assigns?

Scott Taylor mailing_lists at railsnewbie.com
Thu Dec 13 02:17:24 EST 2007


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 


More information about the rspec-users mailing list