Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A Brazenly Opinionated Tour of Ruby Testing

A Brazenly Opinionated Tour of Ruby Testing

An intro to testing in Ruby, featuring MiniTest::Unit and why I don't like RSpec.

Bryce "BonzoESC" Kerley

February 06, 2012
Tweet

More Decks by Bryce "BonzoESC" Kerley

Other Decks in Programming

Transcript

  1. Acceptance Testing Feature: Browsing In order to get interested As

    a reader I want to see some interesting statistics Scenario: Daily step stats Given I am on the home page Then I want to see some statistics
  2. Cucumber •Come up with a feature •Write scenarios •Make scenario

    steps pass •Define step •Implement software so step succeeds
  3. Scenario Definition features/browsing.feature: Feature: Browsing In order to get interested

    As a reader I want to see some interesting statistics Scenario: Daily step stats Given I am on the home page When I click the statistics tab Then I want to see some statistics
  4. Why Not High Level? Fulfilling a scenario may be: •hundreds

    of lines of code •dozens of files •several libraries •multiple languages •different businesses
  5. Unit Testing class SessionTest < ActiveSupport::TestCase test 'logging in' do

    @account = Factory :account @session = Session.new(email: @account.email, password: 'password') valid_session = @session.valid? assert_empty @session.errors assert valid_session end end
  6. Unit Testing class RollTest < ActiveSupport::TestCase context 'a Roll' do

    setup do @roll = Factory :roll end should belong_to :player should 'be findable with the "since" scope' do @new_roll = Factory :roll assert_equal [@new_roll], Roll.since(@roll.created_at) end end end
  7. Comparison Shoulda •nested setups •gem to download •awesome for ORM

    Test::Unit •simpler syntax •included in ruby •predictable
  8. Opinion I, Bryce Kerley, personally like shoulda more. I still

    use RSpec on projects that started with it.
  9. Example I couldn’t get FUSE to work on Lion I

    don’t feel like setting up emacs in Ubuntu
  10. FUSE is Easy class TootFS def contents(path) ['toot.txt'] end def

    file?(path) path == '/toot.txt' end def read_file(path) "toooooooot!\n" end end
  11. First Steps class TestFilesystem < MiniTest::Unit::TestCase def test_creation_with_hash h =

    {:host=>'test.invalid', :pb_port=>12345} Riak::Client.expects(:new).with(h).returns(:slug) fs = RiakFS::Filesystem.new h assert fs assert_equal :slug, fs.riak_client end end