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
  • Select an option

  • Save ragaskar/1378997 to your computer and use it in GitHub Desktop.

Select an option

Save ragaskar/1378997 to your computer and use it in GitHub Desktop.

Revisions

  1. ragaskar revised this gist Nov 19, 2011. 3 changed files with 89 additions and 0 deletions.
    40 changes: 40 additions & 0 deletions 3-after_after_spec.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    #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

    21 changes: 21 additions & 0 deletions example_its_lets.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    # 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
    28 changes: 28 additions & 0 deletions example_without_lets.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    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
  2. @richievos richievos revised this gist Nov 19, 2011. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  3. @richievos richievos created this gist Nov 19, 2011.
    17 changes: 17 additions & 0 deletions after_spec.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    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
    30 changes: 30 additions & 0 deletions before_spec.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    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