Jesper Laursen
2007-Sep-30 21:15 UTC
[rspec-users] Problems with testing nested routes using mocking
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?
Ryan Tucker
2007-Sep-30 21:48 UTC
[rspec-users] Problems with testing nested routes using mocking
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?
Jesper Laursen
2007-Oct-01 05:29 UTC
[rspec-users] Problems with testing nested routes using mocking
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.
Ryan Tucker
2007-Oct-01 05:35 UTC
[rspec-users] Problems with testing nested routes using mocking
Jesper Laursen wrote:> 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. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >You are correct, it should be VirtualHost.should_receive(:find).and_return(@virtual_host) The first, third and fourth errors might be showing up because it should be the same in the it "should find the virtual_host requested" block as well. This is because the object called @virtual_hosts does not have a method called "find" defined.
Jesper Laursen
2007-Oct-01 15:25 UTC
[rspec-users] Problems with testing nested routes using mocking
2007/10/1, Ryan Tucker <rctucker at u.washington.edu>:> Jesper Laursen wrote: > > 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. > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > > You are correct, it should be > > VirtualHost.should_receive(:find).and_return(@virtual_host) > > The first, third and fourth errors might be showing up because it should > be the same in the > > it "should find the virtual_host requested" > > > block as well. This is because the object called @virtual_hosts does > not have a method called "find" defined.Okay, now it seems til work. The problem has what I have to place: @domain.virtual_hosts.should_receive(:find).with("1").and_return(@virtual_host) in the before, because the "do_get" method was using it, in all it-blocks. I just think that my before are pretty big. And I have to copy it for every describtion. But thanks a lot. It works OK.
Jarkko Laine
2007-Oct-01 16:26 UTC
[rspec-users] Problems with testing nested routes using mocking
On 1.10.2007, at 18.25, Jesper Laursen wrote:> Okay, now it seems til work. > The problem has what I have to place: > @domain.virtual_hosts.should_receive(:find).with > ("1").and_return(@virtual_host) > in the before, because the "do_get" method was using it, in all it- > blocks. > > I just think that my before are pretty big. > And I have to copy it for every describtion.You could wrap it in a helper method in a module that you can include and call in the specs instead of repeating all the lines. //jarkko -- Jarkko Laine http://jlaine.net http://dotherightthing.com http://www.railsecommerce.com http://odesign.fi
Jesper Laursen
2007-Oct-01 22:33 UTC
[rspec-users] Problems with testing nested routes using mocking
2007/10/1, Jarkko Laine <jarkko at jlaine.net>:> On 1.10.2007, at 18.25, Jesper Laursen wrote: > > Okay, now it seems til work. > > The problem has what I have to place: > > @domain.virtual_hosts.should_receive(:find).with > > ("1").and_return(@virtual_host) > > in the before, because the "do_get" method was using it, in all it- > > blocks. > > > > I just think that my before are pretty big. > > And I have to copy it for every describtion. > > You could wrap it in a helper method in a module that you can include > and call in the specs instead of repeating all the lines.Yes, I know, but not all the code have to be used every time. Now I have a new problem. It know work in a browser, but if i am running autotest, this error raise: <cite> /opt/local/bin/ruby -S script/spec -O spec/spec.opts spec/views/virtual_hosts/show.rhtml_spec.rb F 1) ActionView::TemplateError in ''/virtual_hosts/show.rhtml should render attributes in <p>'' edit_virtual_host_url failed to generate from {:controller=>"virtual_hosts", :domain_id=>"1001", :action=>"edit"}, expected: {:controller=>"virtual_hosts", :action=>"edit"}, diff: {:domain_id=>"1001"} On line #92 of app/views/virtual_hosts/show.rhtml 89: </p> 90: 91: 92: <%= link_to ''Edit'', edit_virtual_host_path(@virtual_host) %> | 93: <%= link_to ''Back'', virtual_hosts_path %> </cite> my before do in show.rhtml_sprec.rb looks like this <cite> before do @domain = mock_model(Domain) @domain.stub!(:domain_id).and_return("1") @virtual_host = mock_model(VirtualHost) @virtual_host.stub!(:servername).and_return("MyString") assigns[:domains] = @domain assigns[:virtual_host] = @virtual_host end </cite>
Jarkko Laine
2007-Oct-02 07:18 UTC
[rspec-users] Problems with testing nested routes using mocking
On 2.10.2007, at 1.33, Jesper Laursen wrote:> It know work in a browser, but if i am > running autotest, this error raise: > > <cite> > /opt/local/bin/ruby -S script/spec -O spec/spec.opts > spec/views/virtual_hosts/show.rhtml_spec.rb > F > > 1) > ActionView::TemplateError in ''/virtual_hosts/show.rhtml should render > attributes in <p>'' > edit_virtual_host_url failed to generate from > {:controller=>"virtual_hosts", :domain_id=>"1001", :action=>"edit"}, > expected: {:controller=>"virtual_hosts", :action=>"edit"}, diff: > {:domain_id=>"1001"} > On line #92 of app/views/virtual_hosts/show.rhtml > > 89: </p> > 90: > 91: > 92: <%= link_to ''Edit'', edit_virtual_host_path(@virtual_host) %> |I''ve stumbled upon something similar. I''m using Globalize and setting the language parameter in a path_prefix: /en/users/1, etc. Rails itself can pass the language parameter to the REST routes automatically, so I can just say user_path(@user). However, all the view specs bomb just like they do here, and I''m forced to say user_path(:language_id => "en", :id => @user) just to make the specs pass, which is both laborious, error-prone and ugly. //jarkko -- Jarkko Laine http://jlaine.net http://dotherightthing.com http://www.railsecommerce.com http://odesign.fi
David Chelimsky
2007-Oct-02 07:21 UTC
[rspec-users] Problems with testing nested routes using mocking
On 10/2/07, Jarkko Laine <jarkko at jlaine.net> wrote:> On 2.10.2007, at 1.33, Jesper Laursen wrote: > > It know work in a browser, but if i am > > running autotest, this error raise: > > > > <cite> > > /opt/local/bin/ruby -S script/spec -O spec/spec.opts > > spec/views/virtual_hosts/show.rhtml_spec.rb > > F > > > > 1) > > ActionView::TemplateError in ''/virtual_hosts/show.rhtml should render > > attributes in <p>'' > > edit_virtual_host_url failed to generate from > > {:controller=>"virtual_hosts", :domain_id=>"1001", :action=>"edit"}, > > expected: {:controller=>"virtual_hosts", :action=>"edit"}, diff: > > {:domain_id=>"1001"} > > On line #92 of app/views/virtual_hosts/show.rhtml > > > > 89: </p> > > 90: > > 91: > > 92: <%= link_to ''Edit'', edit_virtual_host_path(@virtual_host) %> | > > I''ve stumbled upon something similar. I''m using Globalize and setting > the language parameter in a path_prefix: /en/users/1, etc. Rails > itself can pass the language parameter to the REST routes > automatically, so I can just say user_path(@user). However, all the > view specs bomb just like they do here, and I''m forced to say > user_path(:language_id => "en", :id => @user) just to make the specs > pass, which is both laborious, error-prone and ugly.This is a known issue: http://rubyforge.org/tracker/index.php?func=detail&aid=12963&group_id=797&atid=3149 Patches welcome!> > //jarkko > > -- > Jarkko Laine > http://jlaine.net > http://dotherightthing.com > http://www.railsecommerce.com > http://odesign.fi > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Geoffrey Wiseman
2007-Oct-02 18:48 UTC
[rspec-users] Problems with testing nested routes using mocking
On 10/2/07, David Chelimsky <dchelimsky at gmail.com> wrote:> > This is a known issue: > > > http://rubyforge.org/tracker/index.php?func=detail&aid=12963&group_id=797&atid=3149 > > Patches welcome! >Not sure if that''s related to the problem I''m having with view specs and the dynamic path-generators, but it may be. I tried to run the NetBeans debugger on my spec, but as far as I can see, it can''t introspect into the generated code that routing.rb write_generation performs, which makes it difficult to diagnose the problem. I can dig into routing.rb:423 and the calls beneath it, but when I get to routing.rb:424, step into just goes away and drops me off at a failed test. Does anyone have suggestions for an alternate debugging technique, short of modifying rails code, which is a little more intensive than I''d like right now? I don''t mind trying to diagnose the problem and, if once diagnosed, look into the effort of creating a patch, but at the moment, I don''t even know why my route generation''s failing. - Geoffrey -- Geoffrey Wiseman -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071002/db8fc244/attachment-0001.html