Unit
Testing in Ruby
Overview
This activity will provide you the experience of both completing the
implementation of a Ruby class and creating appropriate unit tests for the
class. The example we will use is a simple directed graph, where vertices are
represented by strings and each vertex has an array holding the names of its
directly connected neighbor vertices.
The Activity
- Download DiGraph.rb
and DiGraphTest.rb.
- DiGraph implements the
directed graph structure by maintaining a Hash that maps each known vertex
to an array containing that vertex's directly connected neighbors. It is
perfectly legal for a vertex to have no neighbors (an empty array). No
vertex may list a neighbor more than once. If you need to brush up on
graphs, especially those represented by adjacency lists such as ours,
consult this PDF file on graphs.
- Add tests to DiGraphTest.rb
to test all the provided methods in DiGraph.rb. Fix any errors your
tests uncover in the implementation.
- Implement the three skeleton
methods in DiGraph.rb (near the end of the file).
- Add tests to DiGraphTest.rb
to test your implemented methods.
Submission
Submit DiGraph.rb and DiGraphTest.rb - to a directory named DiGraph in your git repo.
Examples:
Here is the source from the Queue & Unit Tests for Queue used in the Ruby
Unit Testing slides:
queue.rb
test_queue.rb