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.