Rspec-view-file 不知道我的路线
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
rspec 视图测试确实知道您的路线,所以恕我直言,还有其他问题。在查看您的路线时,我看到一个参数
:name
。您不应该以某种方式指定
:name
吗?看起来它应该指向父帖子,不是吗?所以不太确定这是如何运作的。
看来你想做一些类似的事情
或者显式地构建 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
or build the url explicitly.