Unit testing is a part of development process where each piece of code is tested independently. A developer writes unit test cases and executes as and when the models and controllers are created in your Rails application.
Writing unit tests and executing the written tests ensures the code adheres to the desired functionality. But, executing the test cases once is not enough. A developer needs to execute the unit tests often so that it can make your code rock solid by detecting bugs in your code.
You can test the application’s response without having it to test it through browser as Rails tests can simulate browser requests.
Best Practice 1
Usually, every Rails application interacts with database. Write efficient tests by setting up test database and populate it with sample data. A dedicated test database is used to interact with test data. Proper test environment in the application enables you to confidently play with application without it being affected to development code as well as production code.
Best Practice 2
Setting up test data in Ruby is another area to look at. For good tests, setting up the test data is important. Define and customise fixtures for your test data. Using fixtures, you can populate the testing database with pre-defined data. Fixtures are db independent and written in YAML.
Best Practice 3
Write model tests for your written models in application. Use Rails scaffolding. It is a setup process that creates the model, a migration, controller and views for the new resource in a single operation. This also creates a full test suite following Rails best practices.
Best Practice 4
Write test methods using assertions. An assertion is a line of code that evaluates an object for expected results. Assertion checks or validates expressions written in the code. Some of the examples are as below:
- Is the object NIL?
- Does this value = X?
- Does this value != X?
- Does this code throw an exception?
- Is the password greater than 8 characters?
Each test contains one or more assertions. Writing as many assertions as possible can make the tests useful. Only when the assertions are successful will the test pass. Include a test for everything which could break the code. It is a good practice to have at least one test for each of your validations and at least one test for every method in your model. Write as many assertions as you can think of.
Best Practice 5
The test database needs to be up to date with current database structure. This makes your test runs meaningful. The test helper in Ruby checks whether the database has pending migrations. If migrations are pending, an error will occur indicating the schema is not fully migrated.
Best Practice 6
Run your test by invoking test case file through rake test command. You can also run individual test case or test method by running the test and providing the test method name. This runs all test methods from the test case.
When test fails, you see an F; when a test throws an error you see an E in its place.
Best Practice 7
Write functional tests for your controller. Controllers handle incoming web requests to your application and responds with a rendered view.
Testing area of controllers can be some of the below:
- Is the web request successful?
- Is the user directed to the correct page?
- Is the user logged in successfully?
- Is the appropriate message displayed to the user in the view?
There are 6 request types supported and useful in Rails functional tests:
Best Practice 8
Test your views. Asserting the presence of key HTML elements and their content is a useful way to test the views of your application. This tests the response to your incoming web request. The assert_select assertion allows you to do this.