【问题标题】:Rails: Get error on RSpec testing View edit formRails:在 RSpec 测试查看编辑表单时出错
【发布时间】:2021-12-26 14:32:05
【问题描述】:

我在使用 RSpec 进行测试时遇到了一些错误。这是我的代码:

\spec\views\post_categories\edit.html.haml_spec.rb

require 'rails_helper'

RSpec.describe "post_categories/edit", type: :view do
  before do
    login_user
    @post_category = FactoryBot.create(:post_category)
  end

  it "renders the edit post_category form" do
    render
    assert_select "form[method=?]", "post" do
      assert_select "input[name=?]", "post_category[title]"
      assert_select "textarea[name=?]", "post_category[description]"
    end
  end
end

之后出现这样的错误:

  1. post_categories/edit 呈现编辑 post_category 表单

    失败/错误:= simple_form_for(@post_category) do |f|

    ActionView::Template::Error:
    没有路线匹配 {:action=>"show", :controller=>"post_categories", :locale=>#},缺少必需的键:[:id]

    你的意思是? post_category_url
    admin_post_category_url

@post_category 存在且正确,但 simple_form 在测试中不起作用 为什么@post_category 进入 :locale 以及如何修复?

【问题讨论】:

  • 如果没有被测代码(就像几乎所有问题一样),这个问题就无法回答。我们需要视图和您的路线。

标签: ruby-on-rails ruby testing rspec


【解决方案1】:

我将所有link_to <subject> 更改为link_to <subject>_path(id: subject.id) 为什么需要 rspec 我不知道

【讨论】:

    猜你喜欢
    • 2016-07-16
    • 2013-11-07
    • 1970-01-01
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多