Ruby Stub Variations: OpenStruct
Ruby Stub Variations: TestStub
Ruby Stub Variations: Struct
Ruby Stub Variations: Stubba
In my last entry I described how TestStub provided a concise syntax for defining stubs on the fly. However, I always consider it a win when I can remove code from the codebase without removing functionality.
Ruby provides a Struct class that allows you to define classes on the fly. From the Ruby documentation:
A Struct is a convenient way to bundle a number of attributes together, using accessor methods, without having to write an explicit class.Struct also provides a one line solution to the example test.
def test_values_are_appened_to_insert_statementThe syntax of Struct doesn't seem as natural since the proliferation of using hashes as parameters, but Struct does provide the flexability of creating a new class and initializing at a later time. Struct works well and does not suffer from any of the limitations of the other examples.
statement = Insert.into[:table_name].values do
Struct.new(:to_sql).new('select column1, column2 from table2')
assert_equal "insert into table_name select column1, column2 from table2", statement.to_sql