The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Ruby Stub Variations: TestStub

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Jay Fields

Posts: 765
Nickname: jayfields
Registered: Sep, 2006

Jay Fields is a software developer for ThoughtWorks
Ruby Stub Variations: TestStub Posted: Sep 13, 2006 8:32 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Jay Fields.
Original Post: Ruby Stub Variations: TestStub
Feed Title: Jay Fields Thoughts
Feed URL: http://blog.jayfields.com/rss.xml
Feed Description: Thoughts on Software Development
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Jay Fields
Latest Posts From Jay Fields Thoughts

Advertisement
Ruby Stub Variations: Introduction
Ruby Stub Variations: OpenStruct
Ruby Stub Variations: TestStub

As I mentioned in my last entry, OpenStruct provides the desired simplicity when creating a stub. However, OpenStruct's inability to stub previously defined methods made it painful to work with when stubbing ActiveRecord::Base subclass instances. To resolve this issue my team borrowed some of the code from OpenStruct and wrote their own TestStub class. I previously wrote about this decision so I wont go too deep into detail; however, the general idea is that defining methods, instead of depending on a hash, resolved this issue. The implementation we ended up using was a bit simpler than the previous example though.
class TestStub
def initialize(*should_respond_to)
@table = {}
should_respond_to.each do |item|
create_readers(item) and next if item.kind_of? Hash
create_writer(item) and next if item.kind_of? Symbol
end
end

def create_readers(item)
item.each_pair do |key, val|
self.class.send(:define_method, key.to_sym) { val }
end
end

def create_writer(item)
self.class.send(:define_method, :"#{item}=") { }
end
end
TestStub provided us with the same ease of use that OpenStruct provided, thus the solution to example test is basically the same.
def test_values_are_appened_to_insert_statement
statement = Insert.into[:table_name].values do
TestStub.new(:to_sql=>'select column1, column2 from table2')
end
assert_equal "insert into table_name select column1, column2 from table2", statement.to_sql
end
TestStub never gave us any trouble; however, it was one more piece of code (and tests) to maintain.

Read: Ruby Stub Variations: TestStub

Topic: Ajax and Caching Previous Topic   Next Topic Topic: Ruby Stub Variations: OpenStruct

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use