Skip to content

Conversation

@meltar
Copy link

@meltar meltar commented Apr 16, 2013

I cold really use some help with the sales_person_spec.rb tests. I had a really hard time changing the tests to work with the starting city and total distance that I wanted to pass or return. Right now two of the tests are failing because of the fetch I'm doing in route, but I'm not sure how to make dummy information available for results of calculate.

@jwo
Copy link
Member

jwo commented Apr 19, 2013

What do you think of this?

In the first example, we tell CalculatesRoute.calculate to not only have a message expectation, but to also return the data structure that is expected of it. (note that this is usually a code smell that your code is doing too much in one method).

In thee second example, we stub into calculate and tell it to return the results we want, and assert later that it's present.

  it "should calculate a route via the CalculatesRoute" do
    start = stub(name: "Atlanta, GA")
    last = stub(name: "Marietta, GA")
    cities = [start, last]
    subject.stub(:cities) { cities } 
    CalculatesRoute
      .should_receive(:calculate)
      .with(cities, start)
      .and_return( {route: stub, distance: 100} )
    subject.route("Atlanta, GA")
  end

  it "should return the route from CalculatesRoute" do
    route_stub = [stub("Marietta, GA"), stub("Atlanta, GA")]
    start = stub("Marietta, GA")
    CalculatesRoute.stub(:calculate){ {route: route_stub, distance: 100} }
    subject.route("Atlanta, GA").fetch(:route).should eq(route_stub)
  end

Thoughts? If you can take this and re-submit (or turn it into your own words), I can comb over the full result and give more hints.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants