Created
June 22, 2017 03:23
-
-
Save patmaddox/96900b89c73c01d19a73da064b6075a3 to your computer and use it in GitHub Desktop.
This file contains 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
# Collaboration test... | |
describe Client do | |
it 'calls a_method on a server' do | |
client = Client.new | |
server = double 'server' | |
expect(server).to receive(:a_method).with(1, 2).and_return 3 | |
client.do_it(server) | |
end | |
end | |
# For a server contract test, do you need to write: | |
# - one test for server.a_method(1, 2) | |
# - one test that server.a_method(something, something_else) returns 3 | |
# and are they the same test, or separate tests? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In the case where
Controller
usesDatabase.sum_open_order_totals()
, I imagine that theController
doesn't actually care about the meaning of "sum", except perhaps for the implied property thatsum >= 0
. In that case,Controller
probably doesn't care about the difference betweencount_open_orders()
andsum_open_order_totals()
, so I would let theController
make the same assumptions about both: return value is a non-negative number. TheDatabase
cares about computing the sum correctly, the feature cares about using the sum instead of a count of rows, but theController
probably doesn't care.Accordingly, the
Controller
stubsDatabase.sum_open_order_totals()
to return 0, another non-negative number, to raise an error (if that makes sense) and that's it. Ifsum_open_order_totals()
were reallysum_open_order_totals_as_of(date)
, then I would treat it the same way we did before.It's an open question whether
Database.sum_open_order_totals()
needs "sum" to be part of its contract. I would treat this as an implementation detail until I encountered a situation where I felt doubt. What matters most is thatsum_open_order_totals()
returns a value that is plausible as a sum (>= 0) and raises errors or not as the situation warrants. When we implementDatabase
, we'll write at least one test forsum = reduce (+) 0 (map orders quantity)
that puts in 3 orders with quantities 1, 3, 5 and expects a sum of 9. We can leave that as an implementation detail test until the contract test becomes helpful. For example, do we want to make it an explicit part of the contract that Orders have a quantity property? or do we just want to leave implicit the notion that Orders can be "summed" somehow? I prefer the latter until we judge that that's no longer good enough. Doubt, a failing test, or a customer-reported mistake would prompt me to action.I agree that it feels off to check in a Contract Test that the orders inside the
Database
are summable (even worse, that they respond to:+
! Implementation detail much?!).Does the
Controller
care how theDatabase
calculates the sum of the open orders? Probably not. In that case, letDatabase
own the notion of "sum". The Contract Tests forDatabase
might include a few examples of computing the sum of the open orders, but that's useful more for documentation for future clients, rather than for the currentController
.On To Your React Example
React Component
Add a test for what happens if the stubbed data has the wrong structure. Default values? Blow up?
For a
find()
method returning at most one item, in general, I want to check this:Service object
This appears to wrap a REST endpoint in order to decouple the React component from the data source. In that case:
REST Endpoint
Integrated tests with server and/or VCR-style "integrated tests" with real data + fake transport