<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">
<br><div><div>On Dec 13, 2007, at 2:00 AM, Jonathan Linowes wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font face="Gill Sans" size="3" style="font: 12.0px Gill Sans">is there a way to stub a method that sets an instance variable, so the stub sets it too? </font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" face="'Gill Sans'"><br class="webkit-block-placeholder"></font></div></blockquote><div><br class="webkit-block-placeholder"></div><div>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. </div><div><br class="webkit-block-placeholder"></div><div>Obviously, this idea breaks down in rails. If you wanted something like that, why not write some shared specs like this:</div><div><br class="webkit-block-placeholder"></div><div>it "should find ..." do</div><div> do_action</div><div> @class_name.constantize.should_receive(:find).with(params).and_return @instance_var_contents</div><div>end</div><div><br class="webkit-block-placeholder"></div><div>it "should assign the instance variable to the template" do</div><div> do_action</div><div> assigns[@class_name.underscore].should == @instance_var_contents</div><div>end</div><div><br class="webkit-block-placeholder"></div><div>where @class_name would be a string like "Foo"</div><div><br class="webkit-block-placeholder"></div><div>Scott</div><div><br class="webkit-block-placeholder"></div><br><blockquote type="cite"><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" face="'Gill Sans'">def find_foo</font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" face="'Gill Sans'"> @foo = Foo.find(params[:id]</font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" face="'Gill Sans'">end</font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" face="'Gill Sans'"><br class="webkit-block-placeholder"></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" face="'Gill Sans'"><br class="webkit-block-placeholder"></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" face="'Gill Sans'">...</font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" face="'Gill Sans'">controller.stub!(:find_foo).and_assigns(:foo, "123")</font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" face="'Gill Sans'"><br class="webkit-block-placeholder"></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" face="'Gill Sans'"><br class="webkit-block-placeholder"></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">_______________________________________________</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">rspec-users mailing list</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><a href="mailto:rspec-users@rubyforge.org">rspec-users@rubyforge.org</a></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><a href="http://rubyforge.org/mailman/listinfo/rspec-users">http://rubyforge.org/mailman/listinfo/rspec-users</a></div> </blockquote></div><br></body></html>