Create a Ruby Gem. Real World, play by play. Part 3
Part 3 of the Gem creation tutorial that follows the conception of a real Gem.
I left off in Part 2 creating a way to configure the persistent locations of the Intuit company id and OAuth information. In Part 3, I need to wire up the oauth_client method to retrieve the configuration settings. As in Part 2, these objectives will pose some interesting challenges.
- Back in
base.rbI create 3 methods:token,retrieve, andsend_chainto handle acquiring persistent information. - Next, I want to start testing my design but I need to go back to the
configuration.rbcode as I haven't verified that it does what it is supposed to do. - Next, I spec the default configuration values, which are 2 association levels deep. (
settings.qb_token,settings.qb_secret, etc) - The previous step's spec passes. Lastly, I test a 1-level-deep association.
- I modify the
oauth_clientmethod to use the newretrieveandoauth_consumermethods. - I go back to finish the
create_service_formethod now that theoauth_clientmethod is completed. - I spec
create_service_for, which will also test theoauth_clientmethod.
@account.settings.qb.token)retrieve method returns a string back from the configuration code (say "settings.qb_token"). It then splits the string into an array ([ 'settings', 'qb_token' ]). The array is fed into the send_chain method that will create a 'send_chain' if the array is size is greater than 1. (e.g. @account.send('settings').send('qb_token')).
Hey Minimul, that is a lot of code with no tests? Where are the tests?
There coming next but let me say that I mostly write tests or specs last, not first. I design and architect the application code first. Then I write automated tests to confirm my design. I also generally do not write many finely granular tests but write at a "courser" or higher level, testing many methods in one spec.
I get paid for code that works, not for tests, so my philosophy is to test as little as possible to reach a given level of confidence. — Kent Beck
If you find your testers splitting up functions to support the testing process, you’re destroying your system architecture and code comprehension along with it. Test at a coarser level of granularity. — James O Coplien
retrieve method, I need to test the configuration code. This first spec passes.
send_chain method, which handles the tricky demands of an association level many layers rich.
token method and am just using the retrieve method directly e.g. (retrieve('token'))oauth_client modifications and testing start at the 13:55 mark in the screencast.
create_service_for.
Great, finished and verified the 'oauth_client' and 'create_service_for' methods. This is a good place to stop for Part 3.
In the next installment I will finish and publish the Gem. Please note that the last screencast is already completed so check it out:
- Pushed on 05/06/2014 by Christian
