[rspec-users] describe AddressesController, "handling GET /addresses" do

Sahyoun osahyoun at gmail.com
Thu Nov 22 06:24:58 EST 2007


Pat, thanks. That helped. I'm now trying to get my head around the error:

Spec::Mocks::MockExpectationError in 'AddressesController handling GET
/addresses/1 should be successful'
Mock 'Address_1006' received unexpected message :find with ("1")

My show method in the addresses controller:
  def show
    @address = @company.addresses.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @address }
    end
  end

In the controller spec:

describe AddressesController, "handling GET /addresses/1" do
  before do
    @address = mock_model(Address)
    @company = mock_model(Company)

    Company.stub!(:find_by_id).and_return(@company)
    @company.stub!(:addresses).and_return(@address)
  end

  def do_get
    get :show, :id => "1", :company_id => "1"
  end

  it "should be successful" do
    do_get
    response.should be_success
  end

....


I know that @company.stub!(:addresses).and_return(@address) is incorrect.
I'm trying to work out how I can stub out:
@address = @company.addresses.find(params[:id])

Thanks for any pointers.

Omar


On 21/11/2007, Pat Maddox <pergesu at gmail.com> wrote:
>
> On Nov 21, 2007 1:35 AM, Pat Maddox <pergesu at gmail.com> wrote:
> > On Nov 21, 2007 1:15 AM, Sahyoun <osahyoun at gmail.com> wrote:
> > > Thanks. That helped. I now have:
> > >
> > > before do
> > >     @address = mock_model(Address)
> > >     @company = mock_model(Company)
> > >     Company.stub!(:find_by_id).and_return(@company)
> > >
> > >     @company.stub!(:addresses).and_return(@addresses)
> > >   end
> > >
> > >
> > > with only one error remaining:
> > >
> > > 'AddressesController handling GET /addresses should assign the found
> > > addresses for the view' FAILED
> > > expected: [nil],
> > >      got: nil (using ==)
> > >
> > >  Spec:
> > >   it "should assign the found addresses for the view" do
> > >     do_get
> > >     assigns[:addresses].should == [@addresses]
> > >   end
> > >
> > >
> > > 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.
> >
> > @company.stub!(:addresses).and_return([@addresses])
> >
> > If you're expecting an array, then you need the stub to return an array.
> >
> > Pat
> >
>
> Guh, sorry, should have looked a bit more closely:
>
> @company.stub!(:addresses).and_return([@address])
>
> Pat
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071122/b3f937d1/attachment.html 


More information about the rspec-users mailing list