Rails RSpec form datepicker and select failing test assertion

Viewed 96

I'm getting to grips with testing in rails, and I've been following this tutorial.

However, unlike the tutorial in my form I have a date select field. This seems to be causing a problem where the assert_select doesn't recognise it, and so the test is failing.

The code in new.html.erb_spec:


RSpec.describe "aquatics/new", type: :view do
  current_user = User.first_or_create!(email: 'me@mail.com', password: 'password', password_confirmation: 'password')

  before(:each) do
    assign(:aquatic, Aquatic.new(
        nick_name: "nickname",
        common_name: "common name",
        scientific_name: "scientific name",
        sex: 'male',
        purchased_on: '2022-02-02',
        length: 10,
        max_lifespan: 5,
        user: current_user,
        notes: "These are the testing notes"
    ))
  end

  it "renders new aquatic form" do
    render
    assert_select "form[action=?][method=?]", aquatics_path, "post" do
      assert_select "input[name=?]", "aquatic[nick_name]"
      assert_select "input[name=?]", "aquatic[common_name]"
      assert_select "input[name=?]", "aquatic[scientific_name]"
      assert_select "input[name=?]", "aquatic[purchased_on]"
      assert_select "input[name=?]", "aquatic[sex]"
      assert_select "input[name=?]", "aquatic[length]"
      assert_select "input[name=?]", "aquatic[max_lifespan]"
      assert_select "input[name=?]", "aquatic[notes]"
    end
  end
end

The failure:

  1) aquatics/new renders new aquatic form
     Failure/Error: assert_select "input[name=?]", "aquatic[purchased_on]"
     
     Minitest::Assertion:
       Expected at least 1 element matching "input[name="aquatic[purchased_on]"]", found 0..
       Expected 0 to be >= 1.
     # ./spec/views/aquatics/new.html.erb_spec.rb:26:in `block (3 levels) in <main>'
     # ./spec/views/aquatics/new.html.erb_spec.rb:22:in `block (2 levels) in <main>'

I assume this is something to do with it being a selection field rather than an input, because if I comment out the purchased_on test line it throws the same error for the sex field.

I've tried changing the input[name=?] to date_select[name=?] and similar actions, but no luck. TIA.

1 Answers

date_select adds 3 select tags for Year, Month, and Date

You are verifying input tag, you should verify select tags.

date_select("aquatic", "purchased_on")

Above date_select will generate below html

<select id="aquatic_purchased_on_1i" name="aquatic[purchased_on(1i)]">
    ...
    <option value="2021">2021</option>
    <option value="2022" selected="selected">2022</option>
    <option value="2023">2023</option>
    ...
</select>
<select id="aquatic_purchased_on_2i" name="aquatic[purchased_on(2i)]">
    <option value="1">January</option>
    <option value="2" selected="selected">February</option>
    ...
    <option value="12">December</option>
</select>
<select id="aquatic_purchased_on_3i" name="aquatic[purchased_on(3i)]">
    ...
    <option value="10">10</option>
    <option value="11" selected="selected">11</option>
    <option value="12">12</option>
    ...
</select>

You can verify it as below

assert_select "select[name=?]", "aquatic[purchased_on(1i)]"
assert_select "select[name=?]", "aquatic[purchased_on(2i)]"
assert_select "select[name=?]", "aquatic[purchased_on(3i)]"
Related