gpt4 book ai didi

ruby-on-rails - 测试 View 编辑表单时 Rspec 失败

转载 作者:数据小太阳 更新时间:2023-10-29 06:59:34 25 4
gpt4 key购买 nike

我正在这里开发我的第一个 Rails 应用程序,但生成的两个测试没有通过:

Failures:

1) gardens/edit renders the edit garden form
Failure/Error: assert_select "inputgarden_name[name=?]", "garden[name]"

Minitest::Assertion:
Expected at least 1 element matching "inputgarden_name[name="garden[name]"]", found 0..
Expected 0 to be >= 1.
# ./spec/views/gardens/edit.html.haml_spec.rb:20:in `block (3 levels) in <top (required)>'
# ./spec/views/gardens/edit.html.haml_spec.rb:18:in `block (2 levels) in <top (required)>'

2) gardens/new renders new garden form
Failure/Error: assert_select "inputgarden_name[name=?]", "garden[name]"

Minitest::Assertion:
Expected at least 1 element matching "inputgarden_name[name="garden[name]"]", found 0..
Expected 0 to be >= 1.
# ./spec/views/gardens/new.html.haml_spec.rb:19:in `block (3 levels) in <top (required)>'
# ./spec/views/gardens/new.html.haml_spec.rb:17:in `block (2 levels) in <top (required)>'

Finished in 1.15 seconds (files took 2.74 seconds to load)
34 examples, 2 failures, 17 pending

Failed examples:

rspec ./spec/views/gardens/edit.html.haml_spec.rb:15 # gardens/edit renders the edit garden form
rspec ./spec/views/gardens/new.html.haml_spec.rb:14 # gardens/new renders new garden form

我不确定这是为什么。当我查看测试时,我有点惊讶路径不包含要编辑的 ID(类似于 /gardens/#{@garden.id}/edit)。当我尝试相应地编辑测试时,rspec 无法运行,告诉我 @garden 尚未实例化。

spec/views/gardens/edit.html.haml_spec.rb:

  1 require 'rails_helper'
2
3
4 RSpec.describe "gardens/edit", type: :view do
5 before(:each) do
6 @garden = assign(:garden, Garden.create!(
7 :name => "MyString",
8 :square_feet => 1,
9 :zone => 1,
10 :garden_type => "MyString",
11 :user => nil
12 ))
13 end
14
15 it "renders the edit garden form" do
16 render
17
18 assert_select "form[action=?][method=?]", garden_path(@garden), "post" do
19
20 assert_select "input#garden_name[name=?]", "garden[name]"
21
22 assert_select "input#garden_square_feet[name=?]", "garden[square_feet]"
23
24 assert_select "input#garden_zone[name=?]", "garden[zone]"
25
26 assert_select "input#garden_garden_type[name=?]", "garden[garden_type]"
27
28 assert_select "input#garden_user_id[name=?]", "garden[user_id]"
29 end
30 end
31 end

我需要做什么才能通过这些测试?

编辑:这里是编辑 View

/app/views/gardens/edit.html.haml:

%h1 Editing garden

= render 'form'

= link_to 'Show', @garden
\|
= link_to 'Back', gardens_path

/app/views/gardens/_form.html.haml:

= simple_form_for(@garden) do |f|
= f.error_notification

.form-inputs
= f.input :name
= f.input :square_feet
= f.input :zone
= f.input :garden_type
= f.association :user

.form-actions
= f.button :submit

最佳答案

您包含的错误消息表明未找到给定匹配器的 html 元素。

对于关联,简单表单 gem 默认生成 select 元素而不是 input 元素。 https://github.com/plataformatec/simple_form#associations

您需要将最后一个断言更改为:assert_select "select#garden_user_id[name=?]", "garden[user_id]"

我猜问题出在新 View 的规范中?

关于ruby-on-rails - 测试 View 编辑表单时 Rspec 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36283028/

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