Rspec-view-file 不知道我的路线

发布于 2024-12-14 13:29:37 字数 1196 浏览 0 评论 0原文

我是 rspec 的新手,为 View 编写 rspec-test ,但有一个问题: 测试失败并出现错误:

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

模型的关系很简单:

post has_many :comments
comment belongs_to :post

查看文件:

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

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

rspec 文件:

# 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

应用程序工作正常。但是如果我从routes.rb中删除该行,那么在开发模式下我会得到同样的错误:

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

所以我认为rspec-view-file看起来不知道我的路由,但我不确定。无论如何,我该如何解决这个问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饭团 2024-12-21 13:29:37

rspec 视图测试确实知道您的路线,所以恕我直言,还有其他问题。在查看您的路线时,我看到一个参数 :name

您不应该以某种方式指定 :name 吗?看起来它应该指向父帖子,不是吗?
所以不太确定这是如何运作的。

看来你想做一些类似的事情

= form_for [@post, @comment]

或者显式地构建 url 。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文