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> @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 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 sufficient for the above to pass. My understanding of mocking and stubbing is sketchy at the moment. Any explanation on how to get this to pass would be appreciated.
<br><br>Many thanks,<br>Omar<br><br><div><span class="gmail_quote">On 21/11/2007, <b class="gmail_sendername">Jarkko Laine</b> <<a href="mailto:jarkko@jlaine.net">jarkko@jlaine.net</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;">
<br>On 21.11.2007, at 9.42, Sahyoun wrote:<br><br>> Hello,<br>><br>> I'm working with scaffold generated controller test code for<br>> handling GET requests. Address is the model being tested. Address<br>> belongs_to Company, Company has_many addresses.
<br>> In my addresses_controller I have:<br>><br>> before_filter :get_company<br>><br>> def index<br>> @addresses = @company.addresses.find(:all)<br>> respond_to do |format|<br>> format.html
# index.html.erb<br>> format.xml { render :xml => @addresses }<br>> end<br>> end<br>><br>> private<br>> def get_company<br>> @company = Company.find_by_id(params[:company_id])
<br>> end<br>><br>><br>> My controller spec code for handling GET /addresses:<br>><br>> before do<br>> @company = mock_model(Company)<br>> @addresses = mock("addresses")<br>> @
company.stub!(:addresses).and_return(@addresses)<br>> Company.stub!(:find).and_return(@company)<br>> end<br>><br>> def do_get<br>> get :index, :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>> All of my tests (4) fail:<br>><br>> 4) NoMethodError in 'AddressesController handling GET /addresses
<br>> should be successful'<br>> You have a nil object when you didn't expect it!<br>> The error occurred while evaluating nil.addresses<br>><br>><br>> Please, can someone explain why i'm getting
nil.addresses?<br><br>Since you haven't stubbed Company.find_by_id, it goes to the<br>database, doesn't find a matching row, and returns nil.<br><br>//jarkko<br><br>--<br>Jarkko Laine<br><a href="http://jlaine.net">
http://jlaine.net</a><br><a href="http://dotherightthing.com">http://dotherightthing.com</a><br><a href="http://www.railsecommerce.com">http://www.railsecommerce.com</a><br><a href="http://odesign.fi">http://odesign.fi</a>
<br><br><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>