Add controller tests for resource-save failures
Task
- Add non-happy-path controller tests to check that the
create
and update
actions behave correctly when saving a limerick fails.
Steps for each controller test
- Add test:
test/controllers/limericks_controller_test.rb
- controller test file.
- Copy the corresponding happy-path test.
- Rename the copy to reflect the new expectations:
"should not create invalid limerick"
- name of new create
test.
"should not update invalid limerick"
- name of new update
test.
- Arrange:
- Act:
- Change the
params
to have invalid empty-string values for title
and limerick_text
:
limerick: { title: '', limerick_text: '' }
- Assert:
assert_no_difference
- check that the number of Limerick
objects in the database has not changed.
assert_response
- check that the HTTP response has a 422 status code.
assert_select
- check that the rendered page contains an h1
element with the appropriate text:
- “New Limerick” for the
create
test.
- “Edit Limerick” for the
update
test.
assert_not_nil
- check that a flash['error']
message was set.
- Run tests and inspect coverage report:
rails test -v
- command to run tests and generate coverage report.
coverage/index.html
- coverage report to view in web browser.
Reference Code