-
-
Save richievos/1381739 to your computer and use it in GitHub Desktop.
Spectastrophe #1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe FashionSituation do | |
it "should be inappropriate at a restaurant" do | |
situation = FashionSituation.new(:shirt => false, :shoes => false, :shorts => true, :location => "restaurant") | |
situation.should_not be_appropriate | |
end | |
it "should be inappropriate at work" do | |
situation = FashionSituation.new(:shirt => false, :shoes => false, :shorts => true, :location => "work") | |
situation.should_not be_appropriate | |
end | |
it "should be appropriate at the beach" do | |
situation = FashionSituation.new(:shirt => false, :shoes => false, :shorts => true, :location => "beach") | |
situation.should be_appropriate | |
end | |
end | |
#### OR making this slightly more realistic, and talking about valid versus invalid | |
# that actually is a case where I think having shared setup between tests is appropriate, because your tests really are really only valuable as a whole. a single one doesn't really prove anything | |
describe FashionSituation do | |
before { @situation = FashionSituation.new(:shirt => false, :shoes => false, :shorts => true, :location => "beach") } | |
it "should be invalid at a restaurant" do | |
@situation.location = 'restaurant' | |
situation.should_not be_valid | |
end | |
it "should be invalid at work" do | |
@situation.location = 'work' | |
situation.should_not be_valid | |
end | |
it "should be valid when setup properly" do | |
@situation.should be_valid | |
end | |
end | |
# Though, generally with these you could just have | |
describe FashionSituation do | |
it "should be invalid at a restaurant" do | |
Factory.build(:fashion_situation, :location => 'restaurant').should_not be_valid | |
end | |
it "should be invalid at work" do | |
Factory.build(:fashion_situation, :location => 'work').should_not be_valid | |
end | |
it "should be valid when setup properly" do | |
Factory.build(:fashion_situation, :location => 'beach').should be_valid | |
end | |
end | |
# not 100% sure which of these 2 I prefer, but I think the one with the before is more obvious and involves less hoops |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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