[rspec-users] Problems with testing nested routes using mocking

Jesper Laursen powerlauer at gmail.com
Mon Oct 1 01:29:17 EDT 2007


2007/9/30, Ryan Tucker <rctucker at u.washington.edu>:
> Jesper Laursen wrote:
> > Hello forum
> >
> > I have there to files
> >
> > #----- virtual_host_controller.rb
> > class VirtualHostsController < ApplicationController
> >   before_filter :capture_domain
> >
> >   # GET /domain/1/virtual_hosts/1
> >   def show
> >     @virtual_host = @domain.virtual_hosts.find(params[:id])
> >
> >     respond_to do |format|
> >       format.html # show.rhtml
> >     end
> >   end
> >
> >   private
> >
> >   def capture_domain
> >     if params[:domain_id].blank?
> >       flash[:notice] = 'Need domain.'
> >       redirect_to domains_url
> >     else
> >       @domain = Domain.find(params[:domain_id])
> >     end
> >   end
> > end
> > #----
> >
> > and
> >
> >
> > #----- virtual_host_controller_spec.rb
> > describe VirtualHostsController, "handling GET /domains/1/virtual_hosts/1" do
> >
> >    before do
> >      @domain = mock_model(Domain)
> >      @virtual_hosts = mock("virtual_hosts")
> >      @virtual_host = mock("virtual_host")
> >      Domain.should_receive(:find).with("1").and_return(@domain)
> >      @domain.should_receive(:virtual_hosts).and_return(@virtual_hosts)
> >      @virtual_hosts.should_receive(:find).and_return(@virtual_host)
> >      login_as :admin
> >    end
> >
> >    def do_get
> >      get :show, :id => "1", :domain_id => "1"
> >    end
> >
> >   it "should render show template" do
> >     do_get
> >     response.should render_template('show')
> >   end
> >
> >    it "should find the virtual_host requested" do
> > #     @domain.should_receive(:virtual_hosts).and_return(@virtual_hosts)
> >      @virtual_hosts.should_receive(:find).with('1').and_return(@virtual_host)
> >      do_get
> >    end
> >
> >   it "should assign the found virtual_host for the view" do
> >     do_get
> >     assigns[:virtual_host].should equal(@virtual_host)
> >   end
> > #-----
> >
> > I have a problem with these three it should-cases. How can I make them to work.
> >
> > The error is:
> > should find the virtual_host requested
> > Mock 'virtual_hosts' expected :find with ("1") once, but received it 0 times
> >
> > The routes are like this:
> >  map.resources :domains do |domains|
> >     domains.resources :virtual_hosts
> >   end
> >
> > What can I do?
> > _______________________________________________
> > rspec-users mailing list
> > rspec-users at rubyforge.org
> > http://rubyforge.org/mailman/listinfo/rspec-users
> >
> Correct me if I am wrong, but in the "before do"
>
> @virtual_hosts.should_receive(:find).and_return(@virtual_host)
>
> should be
>
> VirtualHosts.should_receive(:find).and_return(@virtual_host)
>
> Since you are calling find on the class?

If I change it to VirtualHost (an 's' there, give no sense), these
errors raises:


Spec::Mocks::MockExpectationError in 'VirtualHostsController handling
GET /domains/1/virtual_hosts/1 should assign the found virtual_host
for the view'
Mock 'virtual_hosts' received unexpected message :find with ("1")

Spec::Mocks::MockExpectationError in 'VirtualHostsController handling
GET /domains/1/virtual_hosts/1 should find the virtual_host requested'
Mock 'Class' expected :find with (any args) once, but received it 0 times

Spec::Mocks::MockExpectationError in 'VirtualHostsController handling
GET /domains/1/virtual_hosts/1 should render show template'
Mock 'virtual_hosts' received unexpected message :find with ("1")

Spec::Mocks::MockExpectationError in 'VirtualHostsController handling
GET /domains/1/virtual_hosts/1 should be successful'
Mock 'virtual_hosts' received unexpected message :find with ("1")

So maybe you are a bit wrong :)
I found this page,
http://www.vaporbase.com/postings/Rspec_example_for_nested_resource_index_action,
but unfortunately, does he not describe the same things as I am.


More information about the rspec-users mailing list