Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Struct vs test double in ruby

What would the pros and cons be of using an rspec double over a struct? For example

before :each do
  location = double "locatoin"
  location.stub(:id => 1)
end

vs

before :each do
  location = Struct.new("locatoin", :id)
  location.new.id = 1
end
like image 986
user1899140 Avatar asked Dec 12 '12 21:12

user1899140


1 Answers

Test doubles are easier to set up

Slip = Struct.new(:id)
slip = Slip.new(:id => 1)

vs.

slip = double('slip', :id => 1)

Test doubles generate more informative error messages

require 'spec_helper'

class TooLongError < StandardError; end

class Boat
  def moor(slip)
    slip.moor!(self)
  rescue TooLongError
    false
  end
end

describe Boat do
  let(:boat) { subject }

  context "when slip won't accept boat" do
    it "can't be moored" do
      Slip = Struct.new('Slip')
      slip = Slip.new
      boat.moor(slip).should be_false
    end
  end
end
Failure/Error: slip.moor!(self)
 NoMethodError:
   undefined method `moor!' for #<struct Struct::Slip >

vs.

it "can't be moored" do
  slip = double('slip')
  boat.moor(slip).should be_false
end
Failure/Error: slip.moor!(self)
   Double "slip" received unexpected message :moor! with (#<Boat:0x106b90c60>)

Test doubles have better support for testing

class Boat
  def moor(slip)
    slip.dont_care
    slip.moor!(self)
  rescue TooLongError
    false
  end
end

it "can't be moored" do
  Slip = Struct.new('Slip')
  slip = Slip.new
  slip.should_receive(:moor!).and_raise(TooLongError)
  boat.moor(slip).should be_false
end
Failure/Error: slip.dont_care
 NoMethodError:
   undefined method `dont_care' for #<struct Struct::Slip >

vs.

it "can't be moored" do
  slip = double('slip').as_null_object
  slip.should_receive(:moor!).and_raise(TooLongError)
  boat.moor(slip).should be_false
end

0 failures # passed!

That's a few examples -- I'm sure there are more reasons to prefer test doubles.

like image 154
zetetic Avatar answered Oct 02 '22 12:10

zetetic