Search code examples
ruby-on-railsrubyruby-on-rails-3rspecrspec-rails

Rspec-view-file doesn't know about my routes


I'm new in rspec, write rspec-test for View and there's a problem: test fails with error:

 Failure/Error: render
 ActionView::Template::Error:
   No route matches {:controller=>"comments", :action=>"create", :format=>nil}

model's relations are simple:

post has_many :comments
comment belongs_to :post

view file:

# app/views/posts/show.html.haml

#postform
  = form_for @comment do |f|
    = f.text_field :name
    = f.submit "Reply"

rspec file:

# spec/views/posts/show.html.haml_spec.rb
require 'spec_helper'

describe 'posts/show.html.haml' do
  it 'renders the form for a new comment creation' do
    assign(:post, mock_model(Post).as_new_record.as_null_object)
    assign(:comment, mock_model(Comment).as_new_record.as_null_object) 
    render
  end
end

routes.rb

  post '/:name/comments' => 'comments#create', :as => :comments

Application works normal. But if I remove that line from routes.rb, then in development mode I getting same error:

No route matches {:controller=>"comments", :action=>"create", :format=>nil}

So I think it looks like rspec-view-file doesn't know about my routing, but I'm not sure. Anyway, how can I fix that problem?


Solution

  • The rspec view testing do know your routes, so imho something else is wrong. When looking at your route, I see a parameter :name.

    Shouldn't you be specifying :name somehow? It seems like it should be pointing to the parent-post, no? So not quite sure how this could work.

    It seems you want to be doing something like

    = form_for [@post, @comment]
    

    or build the url explicitly.