Skip to content

17 Testing Controllers and Views

Testing Controllers

  1. 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.

  2. 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

  1. View tests make sure that the information the controller sends to the view is displayed correctly to the user.

  2. 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."

  3. Translate Tests The path.rb file translates your plain English scenarios into tests that Cucumber can understand and run.

  4. 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.

  5. 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.

  6. Speed These tests are usually slower because they have to load up the whole page and all its elements, just like a user would.

  7. 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.

  8. 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.