[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 10:44:59 EDT 2007

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
   - 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

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",
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 %>






    (eval):19:in `actor_path'




    c:/dev/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure'




    c:/dev/ruby/lib/ruby/1.8/timeout.rb:48:in `timeout'





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).


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

More information about the rspec-users mailing list