Monday, September 11, 2006

Ruby Stub Variations

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]
assert_equal "insert into table_name select column1, column2 from table2", statement.to_sql
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.

Ruby Stub Variations: OpenStruct
Ruby Stub Variations: TestStub
Ruby Stub Variations: Struct
Ruby Stub Variations: Stubba

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.