![]() This should fool our client into thinking that it is calling the real object implementation when in fact it was actually just mocked. ![]() The way this is done using mock objects, at least in our case, is to create a class that has a get() method and returns mock values. ![]() We can spare ourselves by injecting a mock one instead! You could plug in an actual implementation creating an actual instance with a Facebook app ID etc., but what for? We already all know what agony is involved to do that when it let’s you deviate from the task at hand. This test too will fail because the Facebook object is null. Of course the first step is to get the data from Facebook, so let us to do that first: facebook->get($id) Now let’s write up our implementation of the service. This is expected, as we have not implemented anything yet! If you run this test you will get a failure. $analytics = $this->statusService->getAnaltyics(1, strtotime(""), strtotime("")) We’ll start with the following unit test to define our expectations of how our object is going to be called and how what the return value will look like: statusService = new StatusService() We’ll implement our simple functionality previously mentioned, e.g tabulating likes and comments of a Facebook post. Now that we know what mock objects are, let’s see some examples in action. You don’t have to go through the hassles I just mentioned just to get started working on your task. And how will you simulate all these actions in a range of days if you wanted to show data by day? How about by month? What do you do if the unthinkable happens and Facebook is down at the moment? A mock object can pretend to be the Facebook library and return the data you need. Heck, you still do not have friends! How will you simulate likes and comments? Mock objects offer a better approach than bothering your fellow developers to like or comment on some posts for you. Where will you get your data? Your Facebook developer account is new. Suppose you would want to tabulate the likes and comments of a Facebook post by day.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |