CSC/ECE 517 Fall 2012/ch2a 2w30 an

From PG_Wiki
Jump to: navigation, search

SaaS - 5.2 - FIRST, TDD and getting started with RSpec

Contents

Introduction

Testing plays a major role in the software development cycle. It is a well known fact that mandatory testing of every functionality implemented be done so as to ensure that the code does not break when in production. Testing can be done in four main stages: Unit testing (individual code modules are tested for flaws and re-coded if any errors are found), Integration testing (various interacting modules are put together piece by piece to ensure that seamless integration exists among them), System testing (the system is tested as a whole after it has been fully integrated) and Acceptance testing (potential users test the system for flaws in design or execution and to see if the product matches their expectation).

This article focuses on Unit testing by following the Test Driven Development approach for Ruby using RSpec framework as explained in this SaaS video.

Characteristics of a good Unit test

DSL

Domain Specific Languages (DSLs) are small programming languages that do only a small number of activities within a specific task domain. Thus we can say that a DSL is not general. Examples for DSLs include migration scripts (which are a small set of statements with the sole job of expressing changes in the database schema), Regexes, SQL.

RSpec

RSpec is a testing tool for Ruby [1]. It is based on Behavior Driven Development(BDD) framework and is inspired by JBehave which is another BDD testing framework [2]. RSpec is considered a DSL for writing tests. The notable difference between RSpec and SQL is that, while RSpec is an internal DSL, SQL is stand alone. Internal or embedded DSLs are implemented within another language that acts as their host [3].

Features of RSpec

Apps folder Spec folder
app/models/*.rb spec/models/*_spec.rb
app/controllers/ *_controller.rb spec/controllers/ *_controller_spec.rb
app/views/*/*.html.erb Use Cucumber

User stories are taken as the input to write RSpecs. The following is an example of one such user story:

When I fill in "Search Posts" with "Assignments"
And I press "Search"
Then I should be on the Posts page with Assignment Posts

Once we have the user stories ready, the next is to implement the corresponding code to realize this test case.

The code we wish we had

When the user clicks on the button in the view for search_posts, the controller action receives the form submission and should perform the following actions:

  1. It will call a method that searches the BackChannel App for posts with the specified search term(s)
  2. It then calls the model method to check the database to see if the entered value exists.
  3. If there is a match, a view with the Posts corresponding to the search terms is rendered. If no match is found, there should be a redirection to the homepage with an error message for the user's information.

The RSpec for the above mentioned scenario can be written as follows:

require 'spec_helper'
describe PostController do
  describe 'searching Posts' do
    it 'should call the model method that performs Post search'
    it 'should select the Search Results template for rendering'
    it 'should make the Post search results available to that template'
  end
end

Spec_helper is a file that RSpec creates as a part of the install step [4]. This file ensures that the functions required for testing are loaded. The behavior of the post controller is described in the code block. The main behavior that we are focusing on is searching post. In the code, it is a method that takes in a string parameter. This string describes the actions to be performed that corresponds to the specification given in the user story. Further behaviors can be added to or nested within the main describe block, or new test scripts can be written, as per requirements.

Summary

A good test case must adhere to the FIRST principle (Fast, Independent, Repeatable, Self-checking, Timely). TDD or Test Driven Development is the approach in which test cases are written for code even before the implementation is realized. DSLs or Domain Specific Languages take on a very specialized set of tasks. When unit tests are written using RSpec, which is an embedded DSL for Ruby testing, we usually write the tests for the controller and model methods and leave the view testing to Cucumber. The Spec files are stored in the Spec directory which mimics the application directory structure. RSpec can be used to perform unit and functional testing.

References

  1. http://rspec.info/
  2. http://en.wikipedia.org/wiki/RSpec
  3. http://en.wikipedia.org/wiki/Domain-specific_language
  4. http://jeffkreeftmeijer.com/2011/spec-helpers-bundler-setup-faster-rails-test-suites/

Also see

Other Interesting Links

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox