Skip to content

Instantly share code, notes, and snippets.

@ragaskar
Forked from richievos/1-before_spec.rb
Created November 19, 2011 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ragaskar/1378997 to your computer and use it in GitHub Desktop.
Save ragaskar/1378997 to your computer and use it in GitHub Desktop.
Spectastrophe #1
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe "Something doppelganger" do
describe "RailCar" do
let(:gateway) { RailCar.new :login => "a", :password => "b" }
describe "#commit" do
let(:response) { railcar.send :commit, request, {} }
describe "response" do
subject { response }
context "with a REJECTed rail car" do
let(:request) { gateway.send(:build_auth_request, 9011, "token", {}) }
before {
railcar.stub(:parse).and_return(
:requestID => "123",
:vehicleCodeRaw => "N9",
:requestToken => "AFLAKJFLDKJFSLDKFJSLDFKJn129399NANF)(A)FNnnnfnfnnfnf",
:amount => "90.01",
)
}
its(:name) { should_not be_nil }
end
end
end
end
end
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe "Something doppelganger" do
it "has an name for a REJECTed rail car" do
railcar = RailCar.new :login => "a", :password => "b"
railcar.stub(:parse).and_return(
:requestID => "123",
:vehicleCodeRaw => "N9",
:requestToken => "AFLAKJFLDKJFSLDKFJSLDFKJn129399NANF)(A)FNnnnfnfnnfnf",
:amount => "90.01",
)
request = railcar.send(:build_request, 9011, "token", {})
response = railcar.send(:commit, request, {})
response.name.should_not be_nil
end
end
#We can't do a lot here because the interface seems bad -- this test being gnarly is not rspec's fault.
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe "Something doppelganger" do
#does login/password matter? Why are we setting them to random values instead of using Factory.build?
let(:railcar) { RailCar.new(:login => "a", :password => "b")
context "when it is rejected" do
#Do these values in the parse response matter? It seems like they don't if we're testing only non-nil
#If they don't matter, let's not set them
let(:parse_response) do
{ :requestID => "123",
:vehicleCodeRaw => "N9",
:requestToken => "AFLAKJFLDKJFSLDKFJSLDFKJn129399NANF)(A)FNnnnfnfnnfnf",
:amount => "90.01" }
end
#Are we sending because these methods are private? If so we shouldn't be exercising them in this test.
let(:response) { railcar.send(:commit, request, {}) }
subject { response }
before do
#railcar should not be stubbed if it is unit tested
railcar.stub(:parse).and_return(parse_response)
railcar.send(:build_request, 9011, "token", {})
end
#it would be nice if we had an expected name here, not nil is not useful.
# It would also be nice if this returned a real object instead of a hash if there are multiple values
# If there *are* multiple values, why are we only testing name? If there aren't, why do we have a hash
# just for name?
its(:name) { should_not be_nil }
end
end
#This test isn't going to exhibit the true flexibility of let/it/subject because
# 1) it only tests a single context,
# and 2) it only makes one assertion.
# its/let/subject when used correctly does a few things:
# 1) Keeps your tests strictly single assertion unit tests.
# 2) No lies in it strings because you don't write them
# 3) More concise, less duplication of setup.
# 4) If used correctly, makes it really obvious what inputs are varying that you care about
# Here's a (contrived, although I often see tests work out neatly like this) example
# that shows some of the reasons I like let/it/subject
describe FashionSituation do
describe "#is_appropriate?" do
let(:situation) { FashionSituation.new(:shirt => false, :shoes => false, :shorts => true, :location => location) }
subject { situation.is_appropriate? }
context "at a restaurant" do
let(:location) { "restaurant" }
it { should be_false }
end
context "at work" do
let(:location) { "work" }
it { should be_false }
end
context "at the beach" do
let(:location) { "beach" }
it { should be_true }
end
end
end
describe FashionSituation do
describe "#is_appropriate?" do
context "at a restaurant" do
before do
@situation = FashionSituation.new(:shirt => false, :shoes => false, :shorts => true, :location => "restaurant")
end
it "should be inappropriate" do
@situation.should_not be_appropriate
end
end
context "at work" do
before do
@situation = FashionSituation.new(:shirt => false, :shoes => false, :shorts => true, :location => "work")
end
it "should be inappropriate" do
@situation.should_not be_appropriate
end
end
context "at the beach" do
before do
@situation = FashionSituation.new(:shirt => false, :shoes => false, :shorts => true, :location => "beach")
end
it "should be appropriate" do
@situation.should be_appropriate
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment