On 21.11.2007, at 9.42, Sahyoun wrote: > Hello, > > I'm working with scaffold generated controller test code for > handling GET requests. Address is the model being tested. Address > belongs_to Company, Company has_many addresses. > In my addresses_controller I have: > > before_filter :get_company > > def index > @addresses = @company.addresses.find(:all) > respond_to do |format| > format.html # index.html.erb > format.xml { render :xml => @addresses } > end > end > > private > def get_company > @company = Company.find_by_id(params[:company_id]) > end > > > My controller spec code for handling GET /addresses: > > before do > @company = mock_model(Company) > @addresses = mock("addresses") > @company.stub!(:addresses).and_return(@addresses) > Company.stub!(:find).and_return(@company) > end > > def do_get > get :index, :company_id => 1 > end > > it "should be successful" do > do_get > response.should be_success > end > ............. > > > All of my tests (4) fail: > > 4) NoMethodError in 'AddressesController handling GET /addresses > should be successful' > You have a nil object when you didn't expect it! > The error occurred while evaluating nil.addresses > > > Please, can someone explain why i'm getting nil.addresses?
Since you haven't stubbed Company.find_by_id, it goes to the database, doesn't find a matching row, and returns nil. //jarkko -- Jarkko Laine http://jlaine.net http://dotherightthing.com http://www.railsecommerce.com http://odesign.fi _______________________________________________ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users