17 Testing Controllers and Views
Testing Controllers¶
-
Controller Tests simulate web requests to your controller actions and make sure the controller does what it should, like retrieving, creating, updating, or deleting data.
-
Writing Tests You write tests that say, "When I send this type of request, I expect this to happen." For example, "When I ask to see the list of books, I should actually see it."
Testing Views¶
-
View tests make sure that the information the controller sends to the view is displayed correctly to the user.
-
Using Cucumber Gem allows you to write tests in plain English that describe user interactions with the web pages. Eg., "When I go to the home page, I should see the welcome message."
-
Translate Tests The
path.rbfile translates your plain English scenarios into tests that Cucumber can understand and run. -
Running Cucumber You run the tests with the command
bundle exec cucumber. Cucumber will act out the scenarios, visiting pages and checking if they look and behave as expected. -
Fragility of Tests View tests can be fragile. If you change something small, like a button color, the test might fail even though nothing important is wrong (change styling can affect cucumber tests). They're also very precise and unforgiving even the smallest mistakes.
-
Speed These tests are usually slower because they have to load up the whole page and all its elements, just like a user would.
-
Importance of Pages Because view tests can be slow and fragile, you usually only write them for the most important parts of your application. For example, you might not test a simple "About" page but definitely test the checkout process in an online store.
-
Communication with Clients Cucumber tests serve as a bridge between developers and non-technical clients. They let you show the client that all the things they care about are being tested and work as expected.