[rspec-users] RSpec view spec writing problem (unable to generate url_for in RESTful resource link_to)

Scott Sehlhorst scott at tynerblain.com
Mon Sep 10 12:13:27 EDT 2007


Thanks very much.  I feel like a bit of a fool, now, for the odd error.

Changing from:
      <%= link_to(a.name,actor_path(@customer, at project),{:class=>"show",:title=>"Show
actor details"}) -%>
To
      <%= link_to(a.name,actor_path(@customer, at project,a),{:class=>"show",:title=>"Show
actor details"}) -%>

Caused everything to work like a champ.  And uncovered an interesting bug.
When not specifying the actor 'a', the link is generated (in the page), but
with id=1.  No idea if/when I would have caught that.

Thanks again David, and all the RSpec'ers out there.

Also, thanks for the multi-nesting pointer.

Scott


On 9/10/07, David Chelimsky <dchelimsky at gmail.com> wrote:
>
> On 9/10/07, Scott Sehlhorst <scott at tynerblain.com> wrote:
> > Thanks, first, to everyone who's asked and answered questions on this
> list,
> > and to the creators of RSpec - it is all very helpful.  I've searched
> the
> > mailing list, and had a couple 2hr googling sessions that didn't help me
> > find an answer.
> >
> > I've run into a problem getting my first non-trivial view spec to
> run.  I
> > get an error when trying to generate a link_to() to another
> resource.  Here
> > is an overview of the situation, then the code:
> >
> >
> > I'm using a restful design
> > I'm using nested (multiply nested) resources
> > I am trying to test a show.rhtml view, that includes links (link_to) to
> > resources managed by another controller
> > The use_case model has a many-to-may through relationship with actors,
> and
> > I'm displaying links to the associated actors on the use_case page
> > I can't get the links to generate when running rspec (but it works great
> in
> > the app)
> > I am in the early stages of the project, and want to move forward BDD,
> but I
> > have some existing code that I need to test before I can start moving
> > forward again.
> > My theories:  I need to stub the controller (for the other resource), or
> I
> > need to stub something else.
> >
> > Here's the command I'm running to drive RSpec
> >
> >  ruby script/spec -f s spec/views
> >
> > Here's the error that I get
> >
> >  1)
> > ActionView::TemplateError in 'UseCase showing a use case should display
> the
> > use case identifier'
> >  actor_url failed to generate from {:customer_id=>"1",
> > :controller=>"actors", :action=>"show", :project_id=>"1"}, expecte
> > d: {:controller=>"actors", :action=>"show"}, diff: {:customer_id=>"1",
> > :project_id=>"1"}
> > On line #22 of app/views/use_cases/show.rhtml
> >
> >     19:   <ul>
> >      20:         <% @primary_actors.each do |a| %>
> >     21:         <li>
> >      22:           <%=
> > link_to(a.name
> ,actor_path(@customer, at project),{:class=>"show",:title=>"Show
> > actor details"}) -%>
> >     23:           <%= link_to("Remove
> >
> assignment",participation_path(@customer, at project, at use_case.participations.find_by
> > _actor_id(a)),{:class=>"delete",:title=>"Remove the
> > assignment of this actor to this use case", :confirm => 'Are you sur
> > e you want to remove this assignment?', :method => :delete})-%>
> >      24:         </li>
> >     25:         <% end %>
> >
> >
> >
> #{RAILS_ROOT}/vendor/rails/actionpack/lib/action_controller/routing.rb:1273:in
> > `raise_named_route_error'
> >
> >
> #{RAILS_ROOT}/vendor/rails/actionpack/lib/action_controller/routing.rb:1245:in
> > `generate'
> >
> >
> #{RAILS_ROOT}/vendor/rails/actionpack/lib/action_controller/url_rewriter.rb:104:in
> > `rewrite_path'
> >
> >
> #{RAILS_ROOT}/vendor/rails/actionpack/lib/action_controller/url_rewriter.rb:69:in
> > `rewrite'
> >
> >
> #{RAILS_ROOT}/vendor/rails/actionpack/lib/action_controller/base.rb:522:in
> > `url_for'
> >
> >
> #{RAILS_ROOT}/vendor/rails/actionpack/lib/action_view/helpers/url_helper.rb:27:in
> > `url_for'
> >      (eval):19:in `actor_path'
> >     #{RAILS_ROOT}/app/views/use_cases/show.rhtml:22:in
> > `_run_rhtml_47app47views47use_cases47show46rhtml'
> >     #{RAILS_ROOT}/app/views/use_cases/show.rhtml:20:in
> > `_run_rhtml_47app47views47use_cases47show46rhtml'
> >
> > #{RAILS_ROOT}/vendor/rails/actionpack/lib/action_view/base.rb:326:in
> > `compile_and_render_template'
> >
> > #{RAILS_ROOT}/vendor/rails/actionpack/lib/action_view/base.rb:301:in
> > `render_template'
> >
> > #{RAILS_ROOT}/vendor/rails/actionpack/lib/action_view/base.rb:260:in
> > `render_file'
> >
> >
> #{RAILS_ROOT}/vendor/rails/actionpack/lib/action_controller/base.rb:806:in
> > `render_file'
> >
> >
> #{RAILS_ROOT}/vendor/rails/actionpack/lib/action_controller/base.rb:741:in
> > `render_with_no_layout'
> >
> >
> #{RAILS_ROOT}/vendor/rails/actionpack/lib/action_controller/layout.rb:256:in
> > `render_without_benchmark'
> >
> >
> #{RAILS_ROOT}/vendor/rails/actionpack/lib/action_controller/benchmarking.rb:50:in
> > `render'
> >     c:/dev/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure'
> >
> >
> #{RAILS_ROOT}/vendor/rails/actionpack/lib/action_controller/benchmarking.rb:50:in
> > `render'
> >
> >
> C:/dev/svn/sherpa/vendor/plugins/rspec_on_rails/lib/spec/rails/dsl/behaviour/view.rb:55:in
> > `render'
> >     spec/views/use_cases/show_view_spec.rb:40
> >
> >
> C:/dev/svn/sherpa/vendor/plugins/rspec/lib/spec/dsl/composite_proc_builder.rb:17:in
> > `proc'
> >
> >
> C:/dev/svn/sherpa/vendor/plugins/rspec/lib/spec/dsl/composite_proc_builder.rb:12:in
> > `proc'
> >
> > C:/dev/svn/sherpa/vendor/plugins/rspec/lib/spec/dsl/example.rb:71:in
> > `before_example'
> >
> > C:/dev/svn/sherpa/vendor/plugins/rspec/lib/spec/dsl/example.rb:25:in
> > `run'
> >     c:/dev/ruby/lib/ruby/1.8/timeout.rb:48:in `timeout'
> >
> > C:/dev/svn/sherpa/vendor/plugins/rspec/lib/spec/dsl/example.rb:24:in
> > `run'
> >
> > C:/dev/svn/sherpa/vendor/plugins/rspec/lib/spec/dsl/behaviour.rb:81:in
> > `run'
> >
> > C:/dev/svn/sherpa/vendor/plugins/rspec/lib/spec/dsl/behaviour.rb:75:in
> > `run'
> >
> >
> C:/dev/svn/sherpa/vendor/plugins/rspec/lib/spec/runner/behaviour_runner.rb:58:in
> > `run_behaviours'
> >
> >
> C:/dev/svn/sherpa/vendor/plugins/rspec/lib/spec/runner/behaviour_runner.rb:57:in
> > `run_behaviours'
> >
> >
> C:/dev/svn/sherpa/vendor/plugins/rspec/lib/spec/runner/behaviour_runner.rb:21:in
> > `run'
> >
> >
> C:/dev/svn/sherpa/vendor/plugins/rspec/lib/spec/runner/command_line.rb:17:in
> > `run'
> >      script/spec:4
> >
> > Here's part of my routes.rb, showing the nested resources
> >
> >   map.resources :customers do |customers|
> >     customers.resources :projects do |projects|
> >        projects.resources :actors
> >       projects.resources :use_cases
> >
> >
> > Here's some of the stubbing that I am using to set up the spec
> >
> >      @customer = mock_model(Customer,
> >       :id => 1,
> >        :to_param => "1"
> >       )
> >
> >     @project = mock_model(Project,
> >        :id => 1,
> >       :customer_id => 1,
> >        :to_param => "1"
> >       )
> >
> > I've also stubbed objects to create the following :assigns
> >
> >     assigns[:customer] = @customer
> >      assigns[:project] = @project
> >     assigns[:use_case] = @use_case
> >      assigns[:primary_actors] = @primary_actors
> >
> > I suspect that there is something else that needs to be stubbed - actors
> > controller, maybe?  I really don't know.  If anyone can point me in the
> > right direction, or tell me where in the error message to dig deeper,
> that
> > would be awesome.
> >
> > If I left out any important information, please let me know!  And a huge
> > thanks in advance to anyone who can help me out, or explain why this
> isn't
> > supported, or point me towards any docs or articles that address this
> issue
> > (because I will have it a zillion times, as almost all views in my app
> > display content and links from more than one model).
>
> I don't think it's related to rspec. Note that the top of the error stack
> says:
>
>
>   #{RAILS_ROOT}/vendor/rails/actionpack/lib/action_controller/routing.rb:1273:in
> `raise_named_route_error'
>
> So the error is likely related to this bit:
>
>   actor_path(@customer, at project)
>
> Given the nesting that you're doing multi-level nesting (which is not
> recommended btw - read
> http://weblog.jamisbuck.org/2007/2/5/nesting-resources), I believe
> that the path should be either:
>
> customer_project_actor_path(@customer, @project, @actor)
>
> or
>
> customer_project_actors_path(@customer, @project)
>
> HTH,
> David
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>



-- 
Scott Sehlhorst, President Tyner Blain LLC
http://tynerblain.com/blog
http://tynerblain.com/nexus
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070910/a229d0fe/attachment.html 


More information about the rspec-users mailing list