I'm writing wrapper for REST API of a service. In my wrapper I have quite simple method:
public List<string> GetAvailableSources()
{
var request = new RestRequest($"session/{Id}");
var sessionProto = _client.Execute<SessionPrototype>(request);
return sessionProto.Data.InstalledSources;
}
It's really simple and just sends request to service and returns result. However, unit test for that method is huge:
[Test]
public void GetAvailableSources_MustReturnItemsFromResponse()
{
const long sessionId = 123;
var clientMock = new Mock<IRestClient>();
clientMock.Setup(x => x.Execute<SessionPrototype>(It.IsAny<IRestRequest>()))
.Returns(new RestResponse<SessionPrototype>()
{
Data = new SessionPrototype
{
Id = sessionId,
InstalledSources = new List<string>
{
"Scanner1",
"Scanner2"
}
},
StatusCode = HttpStatusCode.OK
}).Verifiable();
var session = new Session(sessionId, clientMock.Object);
var sources = session.GetAvailableSources();
Assert.That(sources, Has.Count.EqualTo(2));
Assert.That(sources[0], Is.EqualTo("Scanner1"));
Assert.That(sources[1], Is.EqualTo("Scanner2"));
clientMock.Verify(
m => m.Execute<SessionPrototype>(
It.Is<IRestRequest>(
r => r.Resource == $"session/{sessionId}" && r.Method == Method.GET)));
}
Is there a way for simplifying tests for REST API?