How To Write Tests & Stories For Ruby Gems -- Jaoo 2009
Upcoming SlideShare
Loading in...5
×
 

How To Write Tests & Stories For Ruby Gems -- Jaoo 2009

on

  • 5,811 views

You can write a small Ruby library in only a few lines. But then it grows, it expands and then it starts to break and become a maintenance nightmare. Since its open source you just stop working on it. ...

You can write a small Ruby library in only a few lines. But then it grows, it expands and then it starts to break and become a maintenance nightmare. Since its open source you just stop working on it. Users complain that the project has been abandoned. Your project ends up causing more grief for everyone than if you'd never written it at all.

Instead, we will learn to write all Ruby libraries, RubyGems with tests.

This session is NOT about "how to do TDD". More importantly this session will teach you:

* the one command you should run before starting any new Ruby project

* the best way to write tests for command-line apps, Rake tasks and other difficult to test code

* how to do Continuous Integration of your Ruby/Rails libraries with runcoderun.com

Once you know how to write tests for all Ruby code, you'll want to do it for even the smallest little libraries and have the confidence to know your code always works.

Statistics

Views

Total Views
5,811
Views on SlideShare
5,653
Embed Views
158

Actions

Likes
13
Downloads
114
Comments
3

3 Embeds 158

http://coderwall.com 127
http://www.slideshare.net 30
http://localhost:3000 1

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

CC Attribution License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

13 of 3 Post a comment

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • My thoughts about Cucumber and new Capybara DSL, checkout at http://inancgumus.com/66712574
    Are you sure you want to
    Your message goes here
    Processing…
  • Good overview
    Are you sure you want to
    Your message goes here
    Processing…
  • This presentation was very informative.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • Abstract: You can write a small Ruby library in only a few lines. But then it grows, it expands and then it starts to break and become a maintenance nightmare. Since its open source you just stop working on it. Users complain that the project has been abandoned. Your project ends up causing more grief for everyone than if you'd never written it at all. <br /> <br /> Instead, we will learn to write all Ruby libraries, RubyGems with tests. <br /> <br /> This session is NOT about \"how to do TDD\". More importantly this session will teach you: <br /> <br /> * the one command you should run before starting any new Ruby project <br /> <br /> * the best way to write tests for command-line apps, Rake tasks and other difficult to test code <br /> <br /> * how to do Continuous Integration of your Ruby/Rails libraries with runcoderun.com <br /> <br /> Once you know how to write tests for all Ruby code, you'll want to do it for even the smallest little libraries and have the confidence to know your code always works. <br />
  • JAOO.au 2008 had no Ruby talks. It had an F# talk but no Ruby talks. There were Ruby groups all around Australia. Perhaps there are secret F# clubs I don’t know about. <br /> <br /> But this year we have a whole afternoon of Ruby mixed in with the rest of the awesomeness within this conference. <br />
  • <br />
  • Since we’re at JAOO, we want to track conversations on twitter about JAOO. <br /> <br /> Since we’re nerds, let’s pull these into to the command line and print them out. <br /> <br /> Perhaps we’ll also add a “tail” feature to sit there watching for new tweets as they come along. <br /> <br /> A twitter terminal client. Very nerdy. Very JAOO. <br />
  • This would be our sample (truncated) output from the previous example page. <br /> <br /> It will then sit there pulling down search results and printing new search results when they appear. <br /> <br /> We’ll use Ctrl-C to cancel. <br />
  • ‘newgem’ is the one command you should run before starting any new Ruby project or Rails plugin. <br />
  • <br />
  • <br />
  • And we have a working command line application! <br />
  • In agile development we describe a user story. What is it that the user wants from our software system? And what is the value to them for it. <br />
  • <br />
  • story_text is the content from the “Basic user story” slide <br />
  • <br />
  • <br />
  • If this search query is called then the contents of the fixtures fill will be returned. <br />
  • If this search query is called then the contents of the fixtures fill will be returned. <br />
  • Save a real copy of actual data from the target feed into your project. <br />
  • This wires fakeweb into cucumber and asks it to throw errors if we ever ask for remote content that hasn’t been setup via FakeWeb.request_uri <br />
  • So when we run our feature scenarios again we just get the error. <br /> <br /> Why? We haven’t written any code yet; but we’ve finished setting up our integration test. <br />
  • Ben Mabey took 137 slides to discuss when and why to start with Cucumber and then progress to unit tests of sections of your code. <br />
  • Somewhere in our solution code we call out to the json feed, parse the JSON, and print out the results. <br />
  • If I disable my internet and run my tests then I should definitely see fakeweb coming into effect. But it doesn’t. Bugger. <br /> <br /> This is because “When I run local executable...” invokes the executable in new Ruby process. It knows nothing about fakeweb. <br />
  • This is the default implementation of “run local executable”. We actually make a pure external system call to run the app, just like a user. <br />
  • If you do need to stub out something - a remote service, change the clock, speed things up, then its a lot easier to do it within the same Ruby process. <br /> <br /> So we’ll break up our integration test: 1 sanity check to test that the bin/tweettail executable is wired up correctly and pulls down some twitter data. <br /> <br /> The rest of our integration tests will invoke the library code directly within the same Ruby process. <br />
  • If you do need to stub out something - a remote service, change the clock, speed things up, then its a lot easier to do it within the same Ruby process. <br /> <br /> So we’ll break up our integration test: 1 sanity check to test that the bin/tweettail executable is wired up correctly and pulls down some twitter data. <br /> <br /> The rest of our integration tests will invoke the library code directly within the same Ruby process. <br />
  • If you do need to stub out something - a remote service, change the clock, speed things up, then its a lot easier to do it within the same Ruby process. <br /> <br /> So we’ll break up our integration test: 1 sanity check to test that the bin/tweettail executable is wired up correctly and pulls down some twitter data. <br /> <br /> The rest of our integration tests will invoke the library code directly within the same Ruby process. <br />
  • There is a very thin wrapper around the library code which was auto-generated. This helps you trust that it should “Just Work”. So let’s just test the final part instead to test the ultimate result <br />
  • Original version that we couldn’t fake out the remote data... <br />
  • New version where we can. <br /> <br /> The first scenario is a thin sanity check: does our app actually pull down the twitter search data feed and print out some message. We have no idea what it will print out, just the structure. <br /> <br /> The second and all subsequent scenarios will start testing our executable within the ruby runtime, so we can stub out the remote HTTP calls. <br />
  • Now we go and write some code, install the gem locally, and run it. <br />
  • New version where we can. <br /> <br /> The first scenario is a thin sanity check: does our app actually pull down the twitter search data feed and print out some message. We have no idea what it will print out, just the structure. <br /> <br /> The second and all subsequent scenarios will start testing our executable within the ruby runtime, so we can stub out the remote HTTP calls. <br />
  • If you’ve used cucumber with rails you’ll have seen the provided steps for webrat like “When I follow ‘link’” and “When I select ‘some drop down’”. If you use the cucumber in a RubyGem you get many provided step definitions too. <br /> <br /> The basic relationship between then is STDOUT and the file system. Do something which prints to STDOUT or modifies the file system, and then test the output or files. <br />
  • To explore how this is happening, let’s look at it in reverse. When we want to test the STDOUT, we need to be able to view and explore it. So we’re reading the STDOUT from previous steps from a file. The file name is stored in @stdout. <br /> <br /> The reason I store STDOUT into files is so that when a scenario fails, I can easily peruse each generated STDOUT file and look at what was output myself. If I just kept STDOUT in memory between steps then I’d lose that. <br />
  • It is all the When steps that run commands or rake tasks or generators, which in turn creates new files and prints things out to STDOUT. <br /> <br /> We run each of these commands from an external system command, just like the user would do, and then store the STDOUT to a file. Its file name is always stored in @stdout. <br />
  • If we’re saving STDOUT to files, if we’re testing generators or other applications or libraries that create new files, where is a safe place to do that? <br /> <br /> All scenarios get a “safe folder”. You get a tmp folder within your project source folder. <br /> <br /> You even get a fake HOME folder and the $HOME env variable wired to it, incase your application wants to manipulate dot files or other content within a user’s home folder. <br /> <br />
  • <br />
  • Let’s write a scenario first for the -f option <br /> <br /> The aim is to only very vaguely care about “how the hell am I going to implement ‘the sleep period has elapsed’ <br />
  • Let’s drive development now. First, adding a -f option. <br />
  • We’ll need some more sample data from twitter. Here’s what the JSON might look like on a subsequent call to the API. The since_id value in the file name comes from the original sample JSON data - the refresh_url value. <br />
  • Now wire in that sample file using fakeweb. This step is used by all the scenarios. If the refresh urls are never called by the application, the fakeweb stubbing doesn’t care. <br />
  • So we’ve cleared the way to test the polling mechanism. <br /> <br /> But as I read this scenario, I can’t think of a way to run the app “a little bit” get its output, then continue, get its output, etc. <br />
  • So we refactor our scenario to represent something we _can_ test. Its still a nice, readable scenario. In fact its even shorter than before. Extra win! <br />
  • So our fancy new executable runner step still calls the same step as before; but first it hijacks the sleep() method; ultimately raising an Interrupt. <br /> <br /> We’re using mocha here to stub out #sleep. <br />
  • By default mocha’s setup and verification steps aren’t installed as we’re all used to in test/unit and rspec. In cucumber we need to explicitly set this up. So I put this code in its own support file. <br />
  • Once we go and write some code that continually loops and sleeps and loops and sleeps, our scenario will pass. Of course we’ll write some unit tests within this process too. <br />
  • I didn’t show much of the application code because that’s just a bit boring. Its all here on github. And the slides for this presentation, so that you can find the github url will be on slideshare. And a link to the slides on slideshare will be posted on my @drnic twitter account. You can’t _not_ find this code. <br /> <br />
  • Its nice if the default rake task runs all your tests. Since we have unit tests and cucumber scenarios, add this line to your rakefile. Now “rake” will run both. <br /> <br /> This turns out to be very useful when we turn on Continuous Integration. <br />
  • So let’s set up simple continuous integration. That way, each time we commit some new code, it will automatically trigger all your tests to be run. <br /> <br /> Click edit. <br />
  • Click “Service Hooks” <br />
  • <br />
  • <br />
  • The final step on github is to press the Test Hook link. But first, you’ll need to go <br />
  • The final step on github is to press the Test Hook link. But first, you’ll need to go <br />
  • <br />
  • So there’s an epilogue to this story and this application. The moment I finished, flush with confidence that my integration-test-driven application was working, I pushed the gem to rubyforge and then announced it on twitter. Within minutes I got a bug report. <br /> <br /> So testing still isn’t a holy grail. An idiot software developer can’t change his spots apparently. <br />
  • So there’s an epilogue to this story and this application. The moment I finished, flush with confidence that my integration-test-driven application was working, I pushed the gem to rubyforge and then announced it on twitter. Within minutes I got a bug report. <br /> <br /> So testing still isn’t a holy grail. An idiot software developer can’t change his spots apparently. <br />
  • So there’s an epilogue to this story and this application. The moment I finished, flush with confidence that my integration-test-driven application was working, I pushed the gem to rubyforge and then announced it on twitter. Within minutes I got a bug report. <br /> <br /> So testing still isn’t a holy grail. An idiot software developer can’t change his spots apparently. <br />
  • For more generic information about Cucumber, start at its home page, watch some videos, etc. <br /> <br /> To learn more about testing RubyGems, read other people’s gems. Read the newgem code base which self-tests with its own helpers. <br />
  • And if you have any questions about anything, you can attempt to get help by contacting me. <br />

How To Write Tests & Stories For Ruby Gems -- Jaoo 2009 How To Write Tests & Stories For Ruby Gems -- Jaoo 2009 Presentation Transcript

  • How to write tests/ stories for RubyGems Dr Nic Williams mocra.com drnicwilliams.com @drnic $ sudo gem install tweettail $ tweettail jaoo -f
  • JAOO.au loves Ruby
  • http://www.slideshare.com/drnic
  • tweettail $ sudo gem install tweettail $ tweettail jaoo mattnhodges: Come speak with me at JAOO next week - http://jaoo.dk/ Steve_Hayes: @VenessaP I think they went out for noodles. #jaoo theRMK: Come speak with Matt at JAOO next week drnic: reading my own abstract for JAOO presentation
  • New Gem in 2min newgem tweet-tail cd tweet-tail script/generate executable tweettail rake manifest rake install_gem
  • Rakefile $hoe = Hoe.new('tweettail', TweetTail::VERSION) do |p| p.developer('FIXME full name', 'FIXME email') ... end $hoe = Hoe.new('tweettail', TweetTail::VERSION) do |p| p.developer('Dr Nic', 'drnicwilliams@gmail.com') ... end
  • README.rdoc = tweet-tail * FIX (url) = tweet-tail * http://github.com/drnic/tweet-tail
  • New Gem in 2min cont... newgem tweet-tail cd tweet-tail script/generate executable tweettail rake manifest rake install_gem tweettail SUCCESS!! To update this executable, look in lib/tweet-tail/cli.rb
  • User story Feature: Live twitter search results on the command line In order to reduce cost of getting live search results As a twitter user I want twitter search results appearing in the console
  • Describe behaviour in plain text Write a step definition in Ruby Run and watch it fail Fix code Run and watch it pass!
  • Install cucumber sudo gem install cucumber script/generate install_cucumber cp story_text features/command_line_app.feature
  • features/cli.feature Feature: Live twitter search results on command line In order to reduce cost of getting live search results As a twitter user I want twitter search results appearing in the console Scenario: Display current search results Given twitter has some search results for 'jaoo' When I run local executable 'tweettail' with arguments 'jaoo' Then I should see quot;quot;quot; mattnhodges: Come speak with me at JAOO next week... Steve_Hayes: @VenessaP I think they went out for... theRMK: Come speak with Matt at JAOO next week... drnic: reading my own abstract for JAOO presentation... quot;quot;quot;
  • Running scenario $ cucumber features/command_line_app.feature ... 1 scenario 2 skipped steps 1 undefined step You can implement step definitions for missing steps with these snippets: Given /^twitter has some search results for quot;([^quot;]*)quot;$/ do |arg1| pending end
  • features/step_definitions/ twitter_data_steps.rb Given /^twitter has some search results for quot;([^quot;]*)quot;$/ do |query| FakeWeb.register_uri( :get, quot;http://search.twitter.com/search.json?q=#{query}quot;, :file => File.dirname(__FILE__) + quot;/../fixtures/search-#{query}.jsonquot;) end
  • features/step_definitions/ twitter_data_steps.rb mkdir -p features/fixtures curl http://search.twitter.com/search.json?q=jaoo > features/fixtures/search-jaoo.json
  • features/fixtures/search-jaoo.rb { quot;resultsquot;: [ { quot;textquot;: quot;reading my own abstract for JAOO presentationquot;, quot;from_userquot;: quot;drnicquot;, quot;idquot;: 1666627310 }, { quot;textquot;: quot;Come speak with Matt at JAOO next weekquot;, quot;from_userquot;: quot;theRMKquot;, quot;idquot;: 1666334207 }, { quot;textquot;: quot;@VenessaP I think they went out for noodles. #jaooquot;, quot;from_userquot;: quot;Steve_Hayesquot;, quot;idquot;: 1666166639 }, { quot;textquot;: quot;Come speak with me at JAOO next week - http://jaoo.dk/quot;, quot;from_userquot;: quot;mattnhodgesquot;, quot;idquot;: 1664823944, }], quot;refresh_urlquot;: quot;?since_id=1682666650&q=jaooquot;, quot;results_per_pagequot;: 15, quot;next_pagequot;: quot;?page=2&max_id=1682666650&q=jaooquot;
  • features/common/env.rb gem quot;fakewebquot; require quot;fakewebquot; Before do FakeWeb.allow_net_connect = false end
  • Running scenario $ cucumber features/command_line_app.feature ... Scenario: Display current search results Given a safe folder And twitter has some search results for quot;jaooquot; When I run local executable quot;tweettailquot; with arguments quot;jaooquot; Then I should see quot;quot;quot; mattnhodges: Come speak with me at JAOO next week... Steve_Hayes: @VenessaP I think they went out for noodles... theRMK: Come speak with Matt at JAOO next week drnic: reading my own abstract for JAOO presentation quot;quot;quot; 1 scenario 1 failed step 3 passed steps
  • http://www.slideshare.net/bmabey/outsidein-development-with-cucumber
  • fetching JSON feed def initial_json_data Net::HTTP.get(URI.parse(quot;http://search.twitter.com/search.json?q=#{query}quot;)) end
  • fakeweb failure?! Scenario: Display current search results Given a safe folder And twitter has some search results for quot;jaooquot; When I run local executable quot;tweettailquot; with arguments quot;jaooquot; getaddrinfo: nodename nor servname provided, or not known (SocketError) from .../net/http.rb:564:in `open' ... from .../tweet-tail/lib/tweet-tail/tweet_poller.rb:24:in `initial_json_data' from .../tweet-tail/lib/tweet-tail/tweet_poller.rb:9:in `refresh' from .../tweet-tail/lib/tweet-tail/cli.rb:39:in `execute' from .../tweet-tail/bin/tweet-tail:10 Then I dump stdout
  • features/step_definitions/ common_steps.rb When /^I run local executable quot;(.*)quot; with arguments quot;(.*)quot;/ do |exec, arguments| @stdout = File.expand_path(File.join(@tmp_root, quot;executable.outquot;)) executable = File.expand_path(File.join(File.dirname(__FILE__), quot;/../../binquot;, exec)) in_project_folder do system quot;ruby #{executable} #{arguments} > #{@stdout}quot; end end
  • Can I ignore a There’s probably always something you can’t quite test Minimise that layer of code Test the rest bin main lib
  • Can I ignore a There’s probably always something you can’t quite test Minimise that layer of code Test the rest bin main lib 1x sanity check
  • Can I ignore a There’s probably always something you can’t quite test Minimise that layer of code Test the rest bin main lib 1x sanity check all other integration tests on internal code
  • bin/tweettail ed to test this? Do I ne #!/usr/bin/env ruby # # Created on 2009-5-1 by Dr Nic Williams # Copyright (c) 2009. All rights reserved. require 'rubygems' require File.expand_path(File.dirname(__FILE__) + quot;/../lib/tweet-tailquot;) require quot;tweet-tail/cliquot; TweetTail::CLI.execute(STDOUT, ARGV)
  • features/cli.feature ... Scenario: Display current search results Given twitter has some search results for 'jaoo' When I run local executable 'tweettail' with arguments 'jaoo' Then I should see quot;quot;quot; mattnhodges: Come speak with me at JAOO next week... Steve_Hayes: @VenessaP I think they went out for... theRMK: Come speak with Matt at JAOO next week... drnic: reading my own abstract for JAOO presentation... quot;quot;quot;
  • features/cli.feature ... Scenario: Display some search results Given a safe folder And twitter has some search results for quot;jaooquot; When I run local executable quot;tweettailquot; with arguments quot;jaooquot; Then I should see some twitter messages Scenario: Display explicit search results Given a safe folder And twitter has some search results for quot;jaooquot; When I run executable internally with arguments quot;jaooquot; Then I should see quot;quot;quot; mattnhodges: Come speak with me at JAOO next week Steve_Hayes: @VenessaP I think they went out for theRMK: Come speak with Matt at JAOO next week drnic: reading my own abstract for JAOO presentation quot;quot;quot;
  • end result $ rake install_gem $ tweettail jaoo JAOO: Linda R.: I used to be a mathematician - I couldn't very well have started... bengeorge: Global Financial Crisises are cool: jaoo tix down to 250 for 2 days. kflund: First day of work at the JAOO Tutorials in Sydney - visiting the Opera House wa7son: To my Copenhagen Ruby or Java colleagues: Get to meet Ola Bini at JAOO Geek Nights ldaley: I am going to JAOO... awesome. jessechilcott: @smallkathryn it's an IT conference. http://jaoo.com.au/sydney-2009/ . scotartt: Looking forward to JAOO Brisbane next week - http://jaoo.com.au/brisbane-2009/ scotartt: JAOO Brisbane 2009 http://ff.im/-2B5ja gwillis: @tweval I would give #jaoo a 10.0 rowanb: Bags almost packed for Sydney. Scrum User Group then JAOO. Driving there mattnhodges: busy rest of week ahead. Spking @ Wiki Wed. Atlassian booth babe @ JAOO conference Syd Thurs &amp; Fri. Kiama 4 Jase's wedding all w'end #fb pcalcado: searching twiter for #jaoo first impressions. kornys: #jaoo has been excellent so far - though my tutorials have been full of Steve_Hayes: RT @martinjandrews: women in rails - provide child care at #railsconf CaioProiete: Wish I could be at #JAOO Australia...
  • ‘I run executable internally’ step defn When /^I run executable internally with arguments quot;(.*)quot;/ do |arguments| require 'rubygems' require File.dirname(__FILE__) + quot;/../../lib/tweet-tailquot; require quot;tweet-tail/cliquot; @stdout = File.expand_path(File.join(@tmp_root, quot;executable.outquot;)) in_project_folder do TweetTail::CLI.execute(@stdout_io = StringIO.new, arguments.split(quot; quot;)) @stdout_io.rewind File.open(@stdout, quot;wquot;) { |f| f << @stdout_io.read } end end
  • Many provided steps Given /^a safe folder/ do Given /^this project is active project folder/ do Given /^env variable $([w_]+) set to quot;(.*)quot;/ do |env_var, value| Given /quot;(.*)quot; folder is deleted/ do |folder| When /^I invoke quot;(.*)quot; generator with arguments quot;(.*)quot;$/ do |generator, args| When /^I run executable quot;(.*)quot; with arguments quot;(.*)quot;/ do |executable, args| When /^I run project executable quot;(.*)quot; with arguments quot;(.*)quot;/ do |executable, args| When /^I run local executable quot;(.*)quot; with arguments quot;(.*)quot;/ do |executable, args| When /^I invoke task quot;rake (.*)quot;/ do |task| Then /^folder quot;(.*)quot; (is|is not) created/ do |folder, is| Then /^file quot;(.*)quot; (is|is not) created/ do |file, is| Then /^file with name matching quot;(.*)quot; is created/ do |pattern| Then /^file quot;(.*)quot; contents (does|does not) match /(.*)// do |file, does, regex| Then /^(does|does not) invoke generator quot;(.*)quot;$/ do |does_invoke, generator| Then /^I should see$/ do |text| Then /^I should not see$/ do |text| Then /^I should see exactly$/ do |text| Then /^I should see all (d+) tests pass/ do |expected_test_count| Then /^I should see all (d+) examples pass/ do |expected_test_count| Then /^Rakefile can display tasks successfully/ do Then /^task quot;rake (.*)quot; is executed successfully/ do |task|
  • ‘I should see...’ features/step_definitions/common_steps.rb Then /^I should see$/ do |text| actual_output = File.read(@stdout) actual_output.should contain(text) end Then /^I should not see$/ do |text| actual_output = File.read(@stdout) actual_output.should_not contain(text) end Then /^I should see exactly$/ do |text| actual_output = File.read(@stdout) actual_output.should == text end
  • ‘When I do something...’ features/step_definitions/common_steps.rb When /^I run project executable quot;(.*)quot; with arguments quot;(.*)quot;/ do |executable, args| @stdout = File.expand_path(File.join(@tmp_root, quot;executable.outquot;)) in_project_folder do system quot;ruby #{executable} #{arguments} > #{@stdout}quot; end end When /^I invoke task quot;rake (.*)quot;/ do |task| @stdout = File.expand_path(File.join(@tmp_root, quot;tests.outquot;)) in_project_folder do system quot;rake #{task} --trace > #{@stdout}quot; end end
  • ‘Given a safe folder...’ features/support/env.rb Before do @tmp_root = File.dirname(__FILE__) + quot;/../../tmpquot; @home_path = File.expand_path(File.join(@tmp_root, quot;homequot;)) FileUtils.rm_rf @tmp_root FileUtils.mkdir_p @home_path ENV[quot;HOMEquot;] = @home_path end
  • tweettail jaoo -f polling please? how to test polling?
  • features/cli.feature Scenario: Poll for results until app cancelled Given twitter has some search results for quot;jaooquot; When I run executable internally with arguments quot;jaoo -fquot; Then I should see quot;quot;quot; mattnhodges: Come speak with me at JAOO next week Steve_Hayes: @VenessaP I think they went out for theRMK: Come speak with Matt at JAOO next week drnic: reading my own abstract for JAOO presentation quot;quot;quot; When the sleep period has elapsed Then I should see quot;quot;quot; mattnhodges: Come speak with me at JAOO next week Steve_Hayes: @VenessaP I think they went out for theRMK: Come speak with Matt at JAOO next week drnic: reading my own abstract for JAOO presentation CaioProiete: Wish I could be at #JAOO Australia... quot;quot;quot; When I press quot;Ctrl-Cquot; ...
  • adding -f option $ cucumber features/cli.feature:22 ... Scenario: Poll for results until app cancelled Given twitter has some search results for quot;jaooquot; When I run executable internally with arguments quot;jaoo -fquot; invalid option: -f (OptionParser::InvalidOption) lib/tweet-tail/cli.rb module TweetTail::CLI def self.execute(stdout, arguments=[]) options = { :polling => false } parser = OptionParser.new do |opts| opts.on(quot;-fquot;, quot;Poll for new search results each 15 seconds.quot; ) { |arg| options[:polling] = true } opts.parse!(arguments) end app = TweetTail::TweetPoller.new(arguments.shift, options) app.refresh stdout.puts app.render_latest_results end end
  • features/fixtures/ search-jaoo- { quot;resultsquot;: [{ quot;textquot;: quot;Wish I could be at #JAOO Australia...quot;, quot;from_userquot;: quot;CaioProietequot;, quot;idquot;: 1711269079, }], quot;since_idquot;: 1682666650, quot;refresh_urlquot;: quot;?since_id=1711269079&q=jaooquot;, quot;queryquot;: quot;jaooquot; }
  • features/step_definitions/ twitter_data_steps.rb Given /^twitter has some search results for quot;([^quot;]*)quot;$/ do |query| FakeWeb.register_uri( :get, quot;http://search.twitter.com/search.json?q=#{query}quot;, :file => File.expand_path(File.dirname(__FILE__) + quot;/../fixtures/search-#{query}.jsonquot;)) since = quot;1682666650quot; FakeWeb.register_uri( :get, quot;http://search.twitter.com/search.json?since_id=#{since}&q=#{query}quot;, :file => File.expand_path(File.dirname(__FILE__) + quot;/../fixtures/search-#{query}-since-#{since}.jsonquot;)) end
  • hmm, sleep... $ cucumber features/cli.feature:22 ... Scenario: Poll for results until app cancelled Given twitter has some search results for quot;jaooquot; When I run executable internally with arguments quot;jaoo -fquot; Then I should see quot;quot;quot; mattnhodges: Come speak with me at JAOO next week - http://jaoo.dk/ Steve_Hayes: @VenessaP I think they went out for noodles. #jaoo theRMK: Come speak with Matt at JAOO next week drnic: reading my own abstract for JAOO presentation quot;quot;quot; When the sleep period has elapsed Then I should see quot;quot;quot; mattnhodges: Come speak with me at JAOO next week - http://jaoo.dk/ Steve_Hayes: @VenessaP I think they went out for noodles. #jaoo theRMK: Come speak with Matt at JAOO next week drnic: reading my own abstract for JAOO presentation CaioProiete: Wish I could be at #JAOO Australia... quot;quot;quot;
  • features/cli.feature Scenario: Poll for results until app cancelled Given twitter has some search results for quot;jaooquot; When I run executable internally with arguments quot;jaoo -fquot; and wait 1 sleep cycle and quit Then I should see quot;quot;quot; mattnhodges: Come speak with me at JAOO next week Steve_Hayes: @VenessaP I think they went out for... theRMK: Come speak with Matt at JAOO next week drnic: reading my own abstract for JAOO presentation CaioProiete: Wish I could be at #JAOO Australia... quot;quot;quot;
  • features/step_definitions/executable_steps.rb When /^I run executable internally with arguments quot;([^quot;]*)quot; and wait (d+) sleep cycles? and quit$/ do |args, cycles| hijack_sleep(cycles.to_i) When %Q{I run executable internally with arguments quot;#{args}quot;} end features/support/time_machine_helpers.rb module TimeMachineHelper # expects sleep() to be called +cycles+ times, and then raises an Interrupt def hijack_sleep(cycles) results = [*1..cycles] # irrelevant truthy values for each sleep call Kernel::stubs(:sleep).returns(*results).then.raises(Interrupt) end end World(TimeMachineHelper)
  • using mocha require quot;mochaquot; World(Mocha::Standalone) Before do mocha_setup end After do begin mocha_verify ensure mocha_teardown end end features/support/mocha.rb
  • working! $ cucumber features/cli.feature:22 Feature: Live twitter search results on command line In order to reduce cost of getting live search results As a twitter user I want twitter search results appearing in the console Scenario: Poll for results until app cancelled Given twitter has some search results for quot;jaooquot; When I run executable internally with arguments quot;jaoo -fquot; and wait 1 sleep cycle and quit Then I should see quot;quot;quot; mattnhodges: Come speak with me at JAOO next week - http://jaoo.dk/ Steve_Hayes: @VenessaP I think they went out for noodles. #jaoo theRMK: Come speak with Matt at JAOO next week drnic: reading my own abstract for JAOO presentation CaioProiete: Wish I could be at #JAOO Australia... quot;quot;quot; 1 scenario (1 passed) 3 steps (3 passed)
  • Rakefile task :default => [:features] $ rake (in /Users/drnic/Documents/ruby/gems/tweet-tail) /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -w - Ilib:ext:bin:test -e 'require quot;rubygemsquot;; require quot;test/unitquot;; require quot;test/ test_helper.rbquot;; require quot;test/test_tweet_poller.rbquot;' Started .... Finished in 0.002231 seconds. 4 tests, 10 assertions, 0 failures, 0 errors /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -I quot;/Library/ Ruby/Gems/1.8/gems/cucumber-0.3.2/lib:libquot; ... ................. 5 scenarios (5 passed) 17 steps (17 passed) Run unit tests + features
  • 1. Create account with http://runcoderun.com 2. Press ‘Test Hook’
  • 1. Create account with http://runcoderun.com 2. Press ‘Test Hook’
  • How to write tests/ stories for RubyGems Dr Nic Williams mocra.com drnicwilliams.com twitter: @drnic drnic@mocra.com