У меня возникли проблемы с попыткой использовать Moq с RestSharp. Возможно, это мое непонимание Moq, но по какой-то причине я продолжаю получать исключение нулевой ссылки при попытке смоделировать RestResponse.
Вот мой модульный тест.
[Test]
public void GetAll_Method_Throws_exception_if_response_Data_is_Null()
{
var restClient = new Mock<IRestClient>();
restClient.Setup(x => x.Execute(It.IsAny<IRestRequest>()))
.Returns(new RestResponse<RootObjectList>
{
StatusCode = HttpStatusCode.OK,
Content = null
} );
var client = new IncidentRestClient(restClient.Object);
Assert.Throws<Exception>(() => client.GetAll());
}
Вот моя реализация:
public class IncidentRestClient : IIncidentRestClient
{
private readonly IRestClient client;
private readonly string url = "some url here";
public IncidentRestClient()
{
client = new RestClient { BaseUrl = new Uri(url) };
}
public RootObjectList GetAll()
{
var request = new RestRequest("api/now/table/incident", Method.GET) { RequestFormat = DataFormat.Json };
request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
IRestResponse<RootObjectList> response = client.Execute<RootObjectList>(request);
if (response.Data == null)
throw new Exception(response.ErrorException.ToString());
return response.Data;
}
}
По какой-то причине объект ответа имеет значение null. Может быть, я неправильно издеваюсь над возвращаемым объектом?
IncidentRestClient
не принимаетIRestClient
в качестве параметра. Определен ли другой конструктор, который принимает этот параметр? - person Marc Harry   schedule 11.05.2015