Hi folks,
Problem: I'm not sure what I exactly have to mock, when mocking an IRestClient so I actually have some response.Data returned.
Details:
In my simple MonoTouch project, I'm trying to test my ApiService class, which consumes a 3rd party API (lets pretend it's Twitter).
As such, I want to make sure that my code which does stuff with the deserialized json, does work. Likewise, I need to make sure that my code handles unexpected API errors (twitter is down or offline or there's a network issue, etc. etc.).
So - instead of hitting Twitter each time .. and also testing for those unexpected error scenario's, I'm trying to Mock the IRestClient and setting up various success and failure scenario's.
So, I've made sure my ApiService takes an optional dependency of an IRestClient. If one is provided, use that. Otherwise, create a new instance.
public static IRestClient MockRestClient(HttpStatusCode httpStatusCode, string json)
{
var mockIRestClient = new Mock<IRestClient>();
mockIRestClient.Setup(x => x.Execute<RootObject>(It.IsAny<IRestRequest>()))
.Returns(new RestResponse<RootObject>
{
Content = json, // NOTE: can be null (eg. bad result).
StatusCode = httpStatusCode
});
return mockIRestClient.Object;
}
public class MyApiService
{
public MyApiService(IRestClient restClient)
{
_restClient = restClient;
}
public IList<TwitterStatus> GetStatuses()
{
var restClient = _restClient ?? new RestClient();
......
var response = restClient.Execute<RootObject>(restRequest);
if (response.StatusCode == HttpStatusCode.OK && response.Data != null)
{
// Do some stuff with deserialized Data
foreach(var item on response.Data)
{
twitterStatuses.Add(new TwitterStatus(item.User, item.Subject, item.Whatever));
}
}
}
}
And that all works great ... except the Data is null :( I'm making the assumption that when i Mock the Content property, that somehow the internal magic also creates the Data property, instead of me having to also mock that property.
So - do we need to mock that out or is there some trick that we can do, so we basically mock out the content result and the stock RestSharp code grabs that, deserializes and sets the Data?
thank :)
-Jussy-