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.
5. 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
6. New Gem in 2min
newgem tweet-tail
cd tweet-tail
script/generate executable tweettail
rake manifest
rake install_gem
7. 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
9. 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
10. 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
11. Describe behaviour in plain text
Write a step definition in Ruby
Run and watch it fail
Fix code
Run and watch it pass!
13. 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;
14. 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
15. 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
17. 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;
19. 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
21. fetching JSON feed
def initial_json_data
Net::HTTP.get(URI.parse(quot;http://search.twitter.com/search.json?q=#{query}quot;))
end
22. 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
23. 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
24. 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
25. 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
26. 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
27. 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)
28. 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;
29. 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;
30. 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 & 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...
31. ‘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
32. 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|
33. ‘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
34. ‘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
35. ‘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
37. 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;
...
38. 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
39. 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;
}
40. 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
41. 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;
42. 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;
43. 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)
44. 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
45. 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)
46.
47. 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
64. How to write tests/
stories for RubyGems
Dr Nic Williams
mocra.com
drnicwilliams.com
twitter: @drnic
drnic@mocra.com
Notes de l'éditeur
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.
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.
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.
But this year we have a whole afternoon of Ruby mixed in with the rest of the awesomeness within this conference.
Since we’re at JAOO, we want to track conversations on twitter about JAOO.
Since we’re nerds, let’s pull these into to the command line and print them out.
Perhaps we’ll also add a “tail” feature to sit there watching for new tweets as they come along.
A twitter terminal client. Very nerdy. Very JAOO.
This would be our sample (truncated) output from the previous example page.
It will then sit there pulling down search results and printing new search results when they appear.
We’ll use Ctrl-C to cancel.
‘newgem’ is the one command you should run before starting any new Ruby project or Rails plugin.
And we have a working command line application!
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.
story_text is the content from the “Basic user story” slide
If this search query is called then the contents of the fixtures fill will be returned.
If this search query is called then the contents of the fixtures fill will be returned.
Save a real copy of actual data from the target feed into your project.
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
So when we run our feature scenarios again we just get the error.
Why? We haven’t written any code yet; but we’ve finished setting up our integration test.
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.
Somewhere in our solution code we call out to the json feed, parse the JSON, and print out the results.
If I disable my internet and run my tests then I should definitely see fakeweb coming into effect. But it doesn’t. Bugger.
This is because “When I run local executable...” invokes the executable in new Ruby process. It knows nothing about fakeweb.
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.
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.
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.
The rest of our integration tests will invoke the library code directly within the same Ruby process.
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.
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.
The rest of our integration tests will invoke the library code directly within the same Ruby process.
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.
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.
The rest of our integration tests will invoke the library code directly within the same Ruby process.
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
Original version that we couldn’t fake out the remote data...
New version where we can.
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.
The second and all subsequent scenarios will start testing our executable within the ruby runtime, so we can stub out the remote HTTP calls.
Now we go and write some code, install the gem locally, and run it.
New version where we can.
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.
The second and all subsequent scenarios will start testing our executable within the ruby runtime, so we can stub out the remote HTTP calls.
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.
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.
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.
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.
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.
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.
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?
All scenarios get a “safe folder”. You get a tmp folder within your project source folder.
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.
Let’s write a scenario first for the -f option
The aim is to only very vaguely care about “how the hell am I going to implement ‘the sleep period has elapsed’
Let’s drive development now. First, adding a -f option.
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.
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.
So we’ve cleared the way to test the polling mechanism.
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.
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!
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.
We’re using mocha here to stub out #sleep.
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.
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.
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.
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.
This turns out to be very useful when we turn on Continuous Integration.
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.
Click edit.
Click “Service Hooks”
The final step on github is to press the Test Hook link. But first, you’ll need to go
The final step on github is to press the Test Hook link. But first, you’ll need to go
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.
So testing still isn’t a holy grail. An idiot software developer can’t change his spots apparently.
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.
So testing still isn’t a holy grail. An idiot software developer can’t change his spots apparently.
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.
So testing still isn’t a holy grail. An idiot software developer can’t change his spots apparently.
For more generic information about Cucumber, start at its home page, watch some videos, etc.
To learn more about testing RubyGems, read other people’s gems. Read the newgem code base which self-tests with its own helpers.
And if you have any questions about anything, you can attempt to get help by contacting me.