It is common for web applications to interface with external services. When testing, since depending on an external service is very fragile, we end up mocking the interaction with such services. However, once in a while, it is still a good idea to check if the contract between your application and the service is still valid.
For example, this week we had to interact with a SOAP service, let’s call it
KittenInfo (why would someone provide kitten information via a SOAP service is beyond the scope of this blog post). We only need to contact one end-point of the
KittenInfo and it is called
get_details, which receives a kitten identifier and returns kitten information:
Since this API is simple, it is very easy to mock the client whenever it is required by our application. On the other hand, we still need to verify that the integration between
KittenInfo SOAP service and our application works correctly, so we write some tests for it:
describe KittenInfo::Client do it "retrieves kitten details" do client = KittenInfo::Client.new details = client.get_details("gorbypuff") details[:owner].should == "tenderlove" end end
However, since this is actually contacting the SOAP Service, it may make your test suite more fragile and slower, even more in this case, in which the SOAP Service responses take as long as kitten’s staring contests.
One possible solution to this problem is to make use of filter tags to exclude the SOAP integration tests from running, except when explicitly desired. We could do this by simply setting:
describe KittenInfo::Client, external: true do # ... end
Then, in your
spec_helper.rb, just set:
RSpec.configure do |config| config.filter_run_excluding external: true end
Now, running your specs will by default skip all groups that have
:external set to
true. Whenever you tweak the client, or in your builds, you can run those specific tests with:
$ rspec --tag external
$ rspec --tag ~js.
What about you? What is your favorite RSpec trick?