RSPEC
Sep 10, 2015
I've been blessed enough to done some contract work for a company last week. It was great learning opportunity and meeting interesting people. My task was creating newsfeed for the site. It was rails app using haml, scss and coffee script. I wasn't familiar with haml and scss but it was rails app so I wasn't so intimidated. When I complete the feature and push to the master branch I realized that I forget to write rspec.
Before getting into details about RSpec, two things always taken care of. One is setting or knowing test environment config and simply how to run rspec.
For instance, the test database need to be set up.
What it actually does is that it runs any pending migrations on the development environment and updates db?schema.rb The rake db:test:load recreats the test database from the current db/schema.rb. On subsequent attemps, it is a good idea to first run db:test:prepare, as it first checks for pending migrations and warns you appropriately. Basically it handles cloning the database so you don't have to run the migrations against test to update the test database. For more information, check here.
The second important thing is how to run the spec.
The following commend will run every rspec you have under the rspec directory.
I don't have much experience in writing test specs but I never consider spec is difficult. As anything else, once you learn it, it will be piece of cake. Let's face it. No one will complain about quality of the test code and it will be just fine as long as it covers 100% test subject. WRONG! Completely Wrong!
When they review my code, they put special attention to rpec and told me that these specs are not only insufficient but completely wrong. Here is the example of controller specs I wrote
This is seriously wrong. This test is actually using ActiveRecord's 'delete' function rather than controller's 'delete' function. Here is what we can test for delete function:
The rails 3 will provide RSpec scaffolds and the generated controller specs pass along session parameters:
To read more about How To: Test controllers with Rails 3 and 4 (and RSpec), click here
For more information, github rspec section has much useful information than others (like relish).