<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? Is there a before_filter?</div><div><br class="webkit-block-placeholder"></div><div>describe AddressesController, "handling GET /addresses/1" do</div><div> before do</div><div> @address = mock_model(Address)</div><div> @company = mock_model(Company)</div><div><br class="webkit-block-placeholder"></div><div> Company.stub!(:find_by_id).and_return(@company)</div><div><br class="webkit-block-placeholder"></div><div> @company.stub!(:addresses)</div><div> @company.addresses.stub!(:find).with("1").and_return(@address)</div><div> 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 2944</div><div>rspec_on_rails edge revision 2944</div><div>rails edge revision 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> def show<br> @address = @company.addresses.find(params[:id])<br><br> respond_to do |format|<br> format.html # show.html.erb<br> format.xml { render :xml => @address } <br> end<br> end<br><br>In the controller spec:<br><br>describe AddressesController, "handling GET /addresses/1" do<br> before do<br> @address = mock_model(Address) <br> @company = mock_model(Company) <br> <br> Company.stub!(:find_by_id).and_return(@company)<br> @company.stub!(:addresses).and_return(@address)<br> end<br> <br> def do_get<br> get :show, :id => "1", :company_id => "1" <br> end<br><br> it "should be successful" do<br> do_get<br> response.should be_success<br> 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: <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> <<a href="mailto:pergesu@gmail.com"> pergesu@gmail.com</a>> 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 <<a href="mailto:pergesu@gmail.com"> pergesu@gmail.com</a>> wrote:<br>> On Nov 21, 2007 1:15 AM, Sahyoun <<a href="mailto:osahyoun@gmail.com">osahyoun@gmail.com</a>> wrote:<br>> > Thanks. That helped. I now have:<br>> ><br>> > before do <br>> > @address = mock_model(Address)<br>> > @company = mock_model(Company)<br>> > Company.stub!(:find_by_id).and_return(@company)<br>> ><br>> > @company.stub!(:addresses).and_return(@addresses) <br>> > end<br>> ><br>> ><br>> > with only one error remaining:<br>> ><br>> > 'AddressesController handling GET /addresses should assign the found<br>> > addresses for the view' FAILED <br>> > expected: [nil],<br>> > got: nil (using ==)<br>> ><br>> > Spec:<br>> > it "should assign the found addresses for the view" do<br>> > do_get<br>> > assigns[:addresses].should == [@addresses] <br>> > end<br>> ><br>> ><br>> > I thought @company.stub!(:addresses).and_return(@addresses) would be<br>> > sufficient for the above to pass. My understanding of mocking and stubbing<br> > > is sketchy at the moment. Any explanation on how to get this to pass would<br>> > be appreciated.<br>><br>> @company.stub!(:addresses).and_return([@addresses])<br>><br>> If you're expecting an array, then you need the stub to return an array. <br>><br>> Pat<br>><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>