<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>Where does @company come from initially in the show controller? &nbsp;Is there a before_filter?</div><div><br class="webkit-block-placeholder"></div><div>describe AddressesController, "handling GET /addresses/1" do</div><div>&nbsp;&nbsp;before do</div><div>&nbsp;&nbsp; &nbsp;@address = mock_model(Address)</div><div>&nbsp;&nbsp; &nbsp;@company = mock_model(Company)</div><div><br class="webkit-block-placeholder"></div><div>&nbsp;&nbsp; &nbsp;Company.stub!(:find_by_id).and_return(@company)</div><div><br class="webkit-block-placeholder"></div><div>&nbsp;&nbsp; &nbsp;@company.stub!(:addresses)</div><div>&nbsp;&nbsp; &nbsp;@company.addresses.stub!(:find).with("1").and_return(@address)</div><div>&nbsp;&nbsp;end</div><div>end</div><div><br class="webkit-block-placeholder"></div><div>Give that a shot, you might want to make some of those expectations instead of stubs, depending on your style.</div><br><div apple-content-edited="true"> <span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div>Nathan Sutton</div><div><a href="mailto:fowlduck@gmail.com">fowlduck@gmail.com</a></div><div>rspec edge revision&nbsp;2944</div><div>rspec_on_rails edge revision&nbsp;2944</div><div>rails edge revision&nbsp;8186</div><div><br></div></div></div></span></div></span></div></span></div></span><br class="Apple-interchange-newline"> </div><br><div><div>On Nov 22, 2007, at 5:24 AM, Sahyoun wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">Pat, thanks. That helped. I'm now trying to get my head around the error:<br><br>Spec::Mocks::MockExpectationError in 'AddressesController handling GET /addresses/1 should be successful'<br>Mock 'Address_1006' received unexpected message :find with ("1") <br><br>My show method in the addresses controller:<br>&nbsp; def show<br>&nbsp;&nbsp;&nbsp; @address = @company.addresses.find(params[:id])<br><br>&nbsp;&nbsp;&nbsp; respond_to do |format|<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; format.html # show.html.erb<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; format.xml&nbsp; { render :xml =&gt; @address } <br>&nbsp;&nbsp;&nbsp; end<br>&nbsp; end<br><br>In the controller spec:<br><br>describe AddressesController, "handling GET /addresses/1" do<br>&nbsp; before do<br>&nbsp;&nbsp;&nbsp; @address = mock_model(Address)&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; @company = mock_model(Company) <br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; Company.stub!(:find_by_id).and_return(@company)<br>&nbsp;&nbsp;&nbsp; @company.stub!(:addresses).and_return(@address)<br>&nbsp; end<br>&nbsp; <br>&nbsp; def do_get<br>&nbsp;&nbsp;&nbsp; get :show, :id =&gt; "1", :company_id =&gt; "1" <br>&nbsp; end<br><br>&nbsp; it "should be successful" do<br>&nbsp;&nbsp;&nbsp; do_get<br>&nbsp;&nbsp;&nbsp; response.should be_success<br>&nbsp; end<br><br>....<br><br><br>I know that @company.stub!(:addresses).and_return(@address) is incorrect. I'm trying to work out how I can stub out: &nbsp;&nbsp;&nbsp; <br>@address = @company.addresses.find(params[:id])<br><br>Thanks for any pointers.<br><br>Omar<br><br><br><div><span class="gmail_quote">On 21/11/2007, <b class="gmail_sendername">Pat Maddox</b> &lt;<a href="mailto:pergesu@gmail.com"> pergesu@gmail.com</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">On Nov 21, 2007 1:35 AM, Pat Maddox &lt;<a href="mailto:pergesu@gmail.com"> pergesu@gmail.com</a>&gt; wrote:<br>&gt; On Nov 21, 2007 1:15 AM, Sahyoun &lt;<a href="mailto:osahyoun@gmail.com">osahyoun@gmail.com</a>&gt; wrote:<br>&gt; &gt; Thanks. That helped. I now have:<br>&gt; &gt;<br>&gt; &gt; before do <br>&gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp; @address = mock_model(Address)<br>&gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp; @company = mock_model(Company)<br>&gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp; Company.stub!(:find_by_id).and_return(@company)<br>&gt; &gt;<br>&gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp; @company.stub!(:addresses).and_return(@addresses) <br>&gt; &gt;&nbsp;&nbsp; end<br>&gt; &gt;<br>&gt; &gt;<br>&gt; &gt; with only one error remaining:<br>&gt; &gt;<br>&gt; &gt; 'AddressesController handling GET /addresses should assign the found<br>&gt; &gt; addresses for the view' FAILED <br>&gt; &gt; expected: [nil],<br>&gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;got: nil (using ==)<br>&gt; &gt;<br>&gt; &gt;&nbsp;&nbsp;Spec:<br>&gt; &gt;&nbsp;&nbsp; it "should assign the found addresses for the view" do<br>&gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp; do_get<br>&gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp; assigns[:addresses].should == [@addresses] <br>&gt; &gt;&nbsp;&nbsp; end<br>&gt; &gt;<br>&gt; &gt;<br>&gt; &gt; I thought&nbsp;&nbsp;&nbsp;&nbsp; @company.stub!(:addresses).and_return(@addresses) would be<br>&gt; &gt; sufficient for the above to pass. My understanding of mocking and stubbing<br> &gt; &gt; is sketchy at the moment. Any explanation on how to get this to pass would<br>&gt; &gt; be appreciated.<br>&gt;<br>&gt; @company.stub!(:addresses).and_return([@addresses])<br>&gt;<br>&gt; If you're expecting an array, then you need the stub to return an array. <br>&gt;<br>&gt; Pat<br>&gt;<br><br>Guh, sorry, should have looked a bit more closely:<br><br>@company.stub!(:addresses).and_return([@address])<br><br>Pat<br>_______________________________________________<br>rspec-users mailing list <br><a href="mailto:rspec-users@rubyforge.org">rspec-users@rubyforge.org</a><br><a href="http://rubyforge.org/mailman/listinfo/rspec-users">http://rubyforge.org/mailman/listinfo/rspec-users</a><br></blockquote></div><br> _______________________________________________<br>rspec-users mailing list<br><a href="mailto:rspec-users@rubyforge.org">rspec-users@rubyforge.org</a><br>http://rubyforge.org/mailman/listinfo/rspec-users</blockquote></div><br></body></html>