Wednesday, April 13, 2016

Rspec fails when testing view edit form

Leave a Comment

I'm working on my first rails app here and two of the generated tests don't pass:

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 

I'm not sure why this is. When I look at the test, I'm kind of surprised the path doesn't contain an id to edit (something like /gardens/#{@garden.id}/edit). When I try to edit the test accordingly rspec fails to run telling me that @garden isn't instantiated yet.

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 

What do I have to do to make these tests pass?

EDIT: here's the edit views

/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 

1 Answers

Answers 1

The error message that you included indicates that an html element for the given matcher was not found.

For associations Simple Form gem generates select elements instead of input elements by default. https://github.com/plataformatec/simple_form#associations

You need to change the last assertion to: assert_select "select#garden_user_id[name=?]", "garden[user_id]"

I am guessing the issue is occurring in the spec for the new view?

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment