CSC/ECE 517 Fall 2010/ch1 1f ap: Difference between revisions
(→Rspec) |
|||
Line 59: | Line 59: | ||
3. '''Methods''' | 3. '''Methods''' | ||
We can have other methods apart from it() , inside the describe block | We can have other methods apart from it() , inside the describe block | ||
* '''Before and after methods. ''' | |||
* '''Helper methods.''' | |||
Revision as of 21:04, 7 September 2010
Introduction
Benefits of unit test frameworks
- Test Driven Development
Unit testing frameworks for Ruby
Test::Unit
Rspec
Rspec is one more unit test framework available for ruby programmers and this framework adopts Behaviour Driven development paradigm .In this we write test cases that resembles spoken English, even people with little idea about the test cases can comprehend what the test case is doing.
Some keyword that you need to know before jumping into technical aspects about Rspec.
- Expectations - These are assertion statments used inside the test case.
- Example - This is a test method and collection of expectations(assertion statements).
- Example Group - Collection of examples is called Example group and also called test case.
Lets take an example and explain on how to write a unit test case using Rspec framework.
describe "Checking Account" do it "should have a balance greater than 0" do CheckingAccount = Account.new CheckingAccount account.balance.should > Money.new(0, :USD) end it “...” do //some expectations end end
Lets go through elements of this test case aka code example group.
1. it () :: This is called code example and it is similar to test method in Test::unit .This method takes string as an argument and the string describes the functionality/ behaviour we are going to test about the system . it() method has a code block , which is collection of expectations also called assertions.Expectations are enclosed between the do ... end keywords.
General structure of it() method:.
will paste the code later over here , need some formatting.
Arguments to it :: it( ) method takes a single String, an optional Hash and an optional block
2. describe() :: We use describe() method to define example group aka test case in Test::Unit. This acts as an abstraction to many code examples -i,e its a wrapper around multiple it() functions .
General structure of describe() construct looks like this.
need to paste code over here and it requires formatting
Arguments:Arguments passed to describe function are string and they describe the subset of behaviour exhibited by the object.
3. Methods We can have other methods apart from it() , inside the describe block
- Before and after methods.
- Helper methods.
1. Before and after methods.
- before(:[each or all] ):Some times we need to run some code or some setup prior to executing the code examples (test methods).We do this by using the before method, this gets executed before any code examples. It takes either :each or :all as parameter . If we use :each then before method gets executed before running each code example. If we use :all then before gets executed only once at the start before running any code examples.
eg
- after(:[each or all] ) : This is counterpart for before function. If we need to run some code or some clean up post running the code examples, we use this function. It takes either :each or :all as parameter . If we use :each then after method gets executed after running each code example. If we use :all then after method gets executed only once, at the end after running all the code examples.
2. Helper methods:Sometimes we require code that is common across all the code examples.Instead of repeating the same code in each code example.we write helper method and this be utilized by all of the code examples present in the code example group.This method helps us to overcome duplication in the code.Even these helper methods can be used across the example groups.
Need to paste code over here with formaatting
4.Nested code example groups(): To better organize our example group some times we need nesting of example groups and this feature is available in Rspec.
Need to paste code over here with formatting
expectations
These are like assertions in Test::Unit framework and are the most important part of any example. Expectations use language that is very easy to understand and even non programmer can understand what we are doing in then give expectation.
Eg: result.should equal(10) It says the result should be equal to 10. Expectation says at specific point in the execution of a code example, some thing should be in some state. Some more expectations:-
- result.should_not equal(10)
- message.should match(/Its raining today//)
- team.should have(2).players
To gain better understand of RSpec’s expectations, let’s get familiar with their different parts of the expectation. First we will start of with the should and should_not methods and will learn about different types of expression matchers.
should and should_not :
Both of these methods are part of the Object Class(Top Most Parent class in Ruby) , Both of these functions take Expression matcher or Ruby Expression with subset of operators available in Ruby as argument .Here expression matcher is an object as its name suggests it matches the expression.
Eg of Expression matchers.:
1)result.should equal(5) expectation using should method
If the result turns out to be 5 then this expectation returns true.
2)result.should_not equal(5) expectation using should_not method.
If the result turns out to be 5 then this expectation returns false.
To understand more about how ruby interprets this line ,follow the links in appendix section.
Matchers: Expression matcher is an object as its name suggests it matches the expression. There are several categories of matchers in Ruby.
- Built-In Matchers :
Are the ones that gets shipped with Rspec.
1.result.should equal(2) ----> is equal(item) expression matcher 2.prime_numbers.should_not include(8) ----> is include(item) expression matcher. 3.(2 * 5).should == 10 ----> Testing for equality matcher. 4.result.should be_close(4.10, 0.004) ----> This matcher is used for floating point , this says if the result is inbetween 4.10 plus or minus 0.04 the expectation is success. 5.result.should match(/OOLS /) ----> This is a text matcher , and it succeeds if the result contains OOLS as part of the string