[LRUG] Testing SOAs

Gabe da Silveira gabe at websaviour.com
Tue Jun 24 00:38:53 PDT 2014


I totally agree with Mark and Jon on the subject.  VCR is a wonderful tool,
but it's sweet spot is for mocking out *external* services.  It's perfect
in that use case because you really want to have a concrete record of every
nuance of that external connection for when something breaks.  However for
internal services where you control both ends you have better tools
available (logging, exceptions, introspection, etc), and VCR is too blunt a
tool.

The approach that I've taken (which I talked about last month:
https://skillsmatter.com/skillscasts/5283-deprecating-activeresource-alternative-approaches-for-internal-rails-services),
is to use a private gem that is shared between the consumer and provider.
 The core idea in the private gem is guaranteeing serialization of the
service objects, so we aren't just relying on JSON with vague semantics,
but we have real models which are shared and identical on both ends.  This
partially addresses Jon's point about ensuring the interface matches
up—it's not perfect as the gem doesn't know the context of an API response,
but at least it does ensure that the serialization and interpretation of an
object is identical on both sides, and it does so without any brittle
system level tests that could be defeating the purpose of an SOA in the
first place.

With regards to stubbing, since we are using ruby on both ends, we include
some testing tools in the gem.  One is a very lightweight factory system.
 Each model has a corresponding factory which represents a standard object.
 Because these are isolated POROs, it's really just a hash of attributes,
nothing fancy like FactoryGirl needed.  Then, the key bit, is a module
called ClientStubs which uses Mocha under the hood to provide higher level
stubs/expectations that leverage our conventions about how different API
calls are structured.  This lets us stub very tersely just above the HTTP
level, but with sensible defaults and a reasonable amount of flexibility.

-gabe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20140624/ac13c168/attachment-0003.html>


More information about the Chat mailing list