Steve Freeman on What’s Wrong with Most TDD Implementations and Building on SOLID Foundations
API Client TDD with Mocked Responses
In parts one and two , we built some very basic functionality and used TDD with PHPUnit to make sure our classes are well tested. We also learned how to test an abstract class in order to make sure its concrete methods worked. Now, let’s continue building our library.
I took the liberty of implementing the functionality and the test for the abstract API class’ constructor, requiring the URL to be passed in. It’s very similar to what we did with the Diffbot and DiffbotTest classes.
I also added some more simple methods, and testing of the different API instantiations and custom fields for the APIs into the mix with dynamic setters and getters using __call . This seemed like too menial work to bother you with as it’s highly repetitive and ultimately futile at this point, but if you’re curious, please leave a comment below and we’ll go through part2-end > part3-start differences in another post – you can even diff the various files and ask about specific differences in the forums, I’d be happy to answer them to the best of my knowledge, and also take some advice regarding their design. Additionally, I have moved the “runInSeparateProcess” directive from the entire DiffbotTest class to just the test that needs an empty static class, which reduced the duration of the entire testing phase to mere seconds.
If you’re just now joining us, please download the part 3 start branch and catch up.
We mentioned before we would be data mocking in this part. This might sound more confusing than it is, so allow me to clarify. When we request a URL through Diffbot, we expect a certain result. Like, requesting a specific Amazon product, we expect to get the parsed values for that product. However, if we rely on this live data in our tests, we face two problems:
The tests become slower by X, where X is the time required to fetch the data from Amazon
The data can change and break our tests. Suddenly, some information our tests relied upon before can break due to different values being returned.
Because of this, it’s best if we cache the entire response to a given API call offline – headers and all – and use it to fake a response to Guzzle (functionality Guzzle has built in). This way, we can feed Diffbot a fake every time during tests and make sure it gets the same data, thereby giving us consistent results. Matthew Setter wrote about data mocking with Guzzle and PHPUnit before here , if you’d like to take a look.
To get to the testing level we need, we’ll be faking the data that Diffbot returns. Doesn’t this mean that we aren’t effectively testing Diffbot itself but only our ability to parse the data? Exactly, it does. It’s not on us to test Diffbot – Diffbot’s crew does that. What we’re testing here is the ability to initiate API calls and parse the data they return – that’s all.
Continue reading % API Client TDD with Mocked Responses %
Microsoft Announces the Launch of Visual Studio 2013 Online:
Microsoft Announces New Server and Cloud Pricing for Enterprise Users:
Test Driven Development in Asp.Net MVC Architecture:
True Quality Assurance in an Agile World:
Meter and Autoscale Multi-Tenant Applications in Windows Azure:
Microsoft cuts first-generation Surface Pro prices by $100:
Google breaks old promise by working on search banner ads:
Internet begins its move beyond .com, .net, and .edu:
What Does an Architect Do in an Agile Shop?:
Scheduler API in Windows 8.1 and Visual Studio 2013:
Introduction to Windows Store App Development for Android Programmers:
Working with the New Point Of Service APIs in Windows 8.1:
Windows Azure BizTalk Services:
What’s New in the Visual Studio 2013 IDE:
By now, everyone knows about Test-Driven Development and unit testing. But are you using the testing frameworks to their fullest?
Because the ASP.NET Web API leverages the fundamental building blocks of the HTTP protocol, it can provide a way of refactoring your existing ASP.NET applications into an AJAX-enabled, Test Driven Development world.