SlideShare ist ein Scribd-Unternehmen logo
1 von 64
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 & 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

Weitere ähnliche Inhalte

Kürzlich hochgeladen

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 

Kürzlich hochgeladen (20)

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 

Empfohlen

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Empfohlen (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

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

  • 1. How to write tests/ stories for RubyGems Dr Nic Williams mocra.com drnicwilliams.com @drnic $ sudo gem install tweettail $ tweettail jaoo -f
  • 4.
  • 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
  • 8. README.rdoc = tweet-tail * FIX (url) = tweet-tail * http://github.com/drnic/tweet-tail
  • 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!
  • 12. Install cucumber sudo gem install cucumber script/generate install_cucumber cp story_text features/command_line_app.feature
  • 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
  • 16. 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
  • 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 &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...
  • 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
  • 36. tweettail jaoo -f polling please? how to test polling?
  • 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
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56. 1. Create account with http://runcoderun.com 2. Press ‘Test Hook’
  • 57. 1. Create account with http://runcoderun.com 2. Press ‘Test Hook’
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64. How to write tests/ stories for RubyGems Dr Nic Williams mocra.com drnicwilliams.com twitter: @drnic drnic@mocra.com

Hinweis der Redaktion

  1. 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.
  2. 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&#x2019;t know about. But this year we have a whole afternoon of Ruby mixed in with the rest of the awesomeness within this conference.
  3. Since we&#x2019;re at JAOO, we want to track conversations on twitter about JAOO. Since we&#x2019;re nerds, let&#x2019;s pull these into to the command line and print them out. Perhaps we&#x2019;ll also add a &#x201C;tail&#x201D; feature to sit there watching for new tweets as they come along. A twitter terminal client. Very nerdy. Very JAOO.
  4. 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&#x2019;ll use Ctrl-C to cancel.
  5. &#x2018;newgem&#x2019; is the one command you should run before starting any new Ruby project or Rails plugin.
  6. And we have a working command line application!
  7. 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.
  8. story_text is the content from the &#x201C;Basic user story&#x201D; slide
  9. If this search query is called then the contents of the fixtures fill will be returned.
  10. If this search query is called then the contents of the fixtures fill will be returned.
  11. Save a real copy of actual data from the target feed into your project.
  12. This wires fakeweb into cucumber and asks it to throw errors if we ever ask for remote content that hasn&#x2019;t been setup via FakeWeb.request_uri
  13. So when we run our feature scenarios again we just get the error. Why? We haven&#x2019;t written any code yet; but we&#x2019;ve finished setting up our integration test.
  14. 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.
  15. Somewhere in our solution code we call out to the json feed, parse the JSON, and print out the results.
  16. If I disable my internet and run my tests then I should definitely see fakeweb coming into effect. But it doesn&#x2019;t. Bugger. This is because &#x201C;When I run local executable...&#x201D; invokes the executable in new Ruby process. It knows nothing about fakeweb.
  17. This is the default implementation of &#x201C;run local executable&#x201D;. We actually make a pure external system call to run the app, just like a user.
  18. 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&#x2019;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.
  19. 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&#x2019;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.
  20. 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&#x2019;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.
  21. There is a very thin wrapper around the library code which was auto-generated. This helps you trust that it should &#x201C;Just Work&#x201D;. So let&#x2019;s just test the final part instead to test the ultimate result
  22. Original version that we couldn&#x2019;t fake out the remote data...
  23. 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.
  24. Now we go and write some code, install the gem locally, and run it.
  25. 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.
  26. If you&#x2019;ve used cucumber with rails you&#x2019;ll have seen the provided steps for webrat like &#x201C;When I follow &#x2018;link&#x2019;&#x201D; and &#x201C;When I select &#x2018;some drop down&#x2019;&#x201D;. 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.
  27. To explore how this is happening, let&#x2019;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&#x2019;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&#x2019;d lose that.
  28. 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.
  29. If we&#x2019;re saving STDOUT to files, if we&#x2019;re testing generators or other applications or libraries that create new files, where is a safe place to do that? All scenarios get a &#x201C;safe folder&#x201D;. 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&#x2019;s home folder.
  30. Let&#x2019;s write a scenario first for the -f option The aim is to only very vaguely care about &#x201C;how the hell am I going to implement &#x2018;the sleep period has elapsed&#x2019;
  31. Let&#x2019;s drive development now. First, adding a -f option.
  32. We&#x2019;ll need some more sample data from twitter. Here&#x2019;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.
  33. 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&#x2019;t care.
  34. So we&#x2019;ve cleared the way to test the polling mechanism. But as I read this scenario, I can&#x2019;t think of a way to run the app &#x201C;a little bit&#x201D; get its output, then continue, get its output, etc.
  35. 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!
  36. 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&#x2019;re using mocha here to stub out #sleep.
  37. By default mocha&#x2019;s setup and verification steps aren&#x2019;t installed as we&#x2019;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.
  38. Once we go and write some code that continually loops and sleeps and loops and sleeps, our scenario will pass. Of course we&#x2019;ll write some unit tests within this process too.
  39. I didn&#x2019;t show much of the application code because that&#x2019;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&#x2019;t _not_ find this code.
  40. 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 &#x201C;rake&#x201D; will run both. This turns out to be very useful when we turn on Continuous Integration.
  41. So let&#x2019;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.
  42. Click &#x201C;Service Hooks&#x201D;
  43. The final step on github is to press the Test Hook link. But first, you&#x2019;ll need to go
  44. The final step on github is to press the Test Hook link. But first, you&#x2019;ll need to go
  45. So there&#x2019;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&#x2019;t a holy grail. An idiot software developer can&#x2019;t change his spots apparently.
  46. So there&#x2019;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&#x2019;t a holy grail. An idiot software developer can&#x2019;t change his spots apparently.
  47. So there&#x2019;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&#x2019;t a holy grail. An idiot software developer can&#x2019;t change his spots apparently.
  48. For more generic information about Cucumber, start at its home page, watch some videos, etc. To learn more about testing RubyGems, read other people&#x2019;s gems. Read the newgem code base which self-tests with its own helpers.
  49. And if you have any questions about anything, you can attempt to get help by contacting me.