我正在处理我的第一个rails应用程序,并且两个生成的测试没有通过:
@H_301_2@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=?]",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还没有实例化.
规格/视图/花园/ edit.html.haml_spec.rb:
@H_301_2@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我需要做些什么才能通过这些测试?
编辑:这是编辑视图
/app/views/gardens/edit.html.haml:
@H_301_2@%h1 Editing garden = render 'form' = link_to 'Show',@garden \| = link_to 'Back',gardens_path/app/views/gardens/_form.html.haml:
@H_301_2@= 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元素.
对于关联,Simple Form gem默认生成select元素而不是input元素. https://github.com/plataformatec/simple_form#associations
您需要将最后一个断言更改为:
assert_select“select#garden_user_id [name =?]”,“garden [user_id]”
我猜这个问题出现在新视图的规范中?