View Testing
Explore how to test views in Ruby on Rails by using the assert_select method to verify the presence and content of HTML elements. Learn to write tests that confirm elements like titles, headers, and forms appear correctly or are absent as needed in your views. This lesson helps you ensure your Rails views render as expected through controller test files.
We'll cover the following...
View testing involves checking the presence or absence of certain key HTML elements used in your views and the contents of these HTML elements.
The assertions made for view testing are done through assert_select.
The tests for views reside in test/controllers/ and are written in the file for the associated controller.
The tests for the app/views/links will be written in the test/controllers/links_controller_test.rb file.
The assert_select method
Before you can process with writing tests for your views, you will have to learn a new assertion: assert_select.
The assert_select method provides you with ways to query HTML elements in your tests.
assert_select 'title'
The assertion above will look for the <title> HTML element in view.
The method selects elements and allows you to perform one or more equality tests, such ...