gpt4 book ai didi

ruby-on-rails - Rspec-view-file 不知道我的路线

转载 作者:数据小太阳 更新时间:2023-10-29 08:05:44 26 4
gpt4 key购买 nike

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

路线.rb

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

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

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

所以我认为 rspec-view-file 似乎不知道我的路由,但我不确定。不管怎样,我该如何解决这个问题?

最佳答案

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

你不应该以某种方式指定 :name 吗?它似乎应该指向父帖子,不是吗?所以不太确定这是如何工作的。

看来你想做类似的事情

= form_for [@post, @comment]

或显式构建 url。

关于ruby-on-rails - Rspec-view-file 不知道我的路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8061786/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com