The majority of my
Unit Tests focus on testing individual classes one method or behavior at a time. To facilitate this type of testing the class under test often collaborates with stub classes. Ruby offers several choices for stubbing.
To demonstrate usage for the various types of stubs I'll use a test from our test suite and show the differing implementations. The example comes from our tests that ensure our SQL DSL behaves as expected. This test verifies that
to_sql
is called on the object that is in the block being passed to the
values
method and the return value of
to_sql
is appended to the sql being generated by the Insert instance.
def test_values_are_appened_to_insert_statement
statement = Insert.into[:table_name].values do
Select[:column1, :column2].from[:table2]
end
assert_equal "insert into table_name select column1, column2 from table2", statement.to_sql
end
The above code uses both the Insert and Select classes within the test. Using both classes does produce a passing test. However, a more robust implementation would allow the behavior of Select to be changed without breaking any of the tests within Insert. The next several entries will focus on the various types of stubs I've used in the past to solve this issue.