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_sqlis called on the object that is in the block being passed to the
valuesmethod and the return value of
to_sqlis appended to the sql being generated by the Insert instance.
def test_values_are_appened_to_insert_statementThe 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.
statement = Insert.into[:table_name].values do
assert_equal "insert into table_name select column1, column2 from table2", statement.to_sql
Ruby Stub Variations: OpenStruct
Ruby Stub Variations: TestStub
Ruby Stub Variations: Struct
Ruby Stub Variations: Stubba