Reputation: 1234
I need to unit test Test1() as below and have to mock msgraph-sdk create event which has http call.
I was mocking Request() and AddAsync( ) methods but got null value on Act.
What is the correct way to unit test CreateAsync by mocking ?
public interface IApiClient
{
Task<Event> CreateAsync(EventItem eventItem);
}
public class ApiClient : IApiClient
{
private readonly IGraphServiceClient _graphServiceClient;
public ApiClient(IGraphServiceClient graphServiceClient)
{
_graphServiceClient = graphServiceClient;
}
public async Task<Event> CreateAsync(EventItem eventItem)
{
var item = new Event
{
Subject = eventItem.Subject
};
return await _graphServiceClient.Users[eventItem.UserInfo.Id].Events
.Request()
.AddAsync(item);
}
}
public class ApiClientTest
{
private Mock<IGraphServiceClient> _graphServiceClientMock;
private IApiClient _apiClient;
private Mock<IUserEventsCollectionRequest> _mockUserEventsCollectionRequest;
private EventItem _eventItem;
[SetUp]
public async System.Threading.Tasks.Task SetupAsync()
{
var x = new Microsoft.Graph.Event()
{
Subject = "123"
};
_graphServiceClientMock = new Mock<IGraphServiceClient>();
_mockUserEventsCollectionRequest = new Mock<IUserEventsCollectionRequest>();
_apiClient = new IApiClient(_graphServiceClientMock.Object);
_graphServiceClientMock.Setup(g => g.Users[_eventItem.UserInfo.Id].Events
.Request())
.Returns(
_mockUserEventsCollectionRequest.Setup(g => g.AddAsync(x))
.ReturnsAsync(new Event { Id = "asaasaS" });
}
[Test]
public async Task Test1()
{
// Act
var newEvent = await _apiClient.CreateAsync(_eventItem);
// Assert
Assert.NotNull(newEvent);
Assert.AreEqual("asaasaS", newEvent.Id);
}
}
I was mocking Request() and AddAsync( ) methods but got null value on Act.
What is the correct way to unit test CreateAsync by mocking ?
Upvotes: 1
Views: 1644
Reputation: 4140
There is a lot of mocking for you to test this, so maybe you should ask yourself is it worth it. If the answer is yes, then here is an example, how you can do that (I put everything into Test1
method and skipped [SetUp]
method for better visibility).
public class ApiClientTest
{
[Test]
public async Task Test1()
{
// Arrange
Event @event = new Event { Subject = "Subject", Id = "123" };
EventItem eventItem = new EventItem { Subject = @event.Subject, UserInfo = new UserInfo { Id = @event.Id } };
Mock<IUserEventsCollectionRequest> mockUserEventsCollectionRequest = new Mock<IUserEventsCollectionRequest>();
mockUserEventsCollectionRequest.Setup(x => x.AddAsync(It.IsAny<Event>())).ReturnsAsync(@event);
Mock<IUserEventsCollectionRequestBuilder> mockUserEventsCollectionRequestBuilder = new Mock<IUserEventsCollectionRequestBuilder>();
mockUserEventsCollectionRequestBuilder.Setup(x => x.Request()).Returns(mockUserEventsCollectionRequest.Object);
Mock<IUserRequestBuilder> userRequestBuilder = new Mock<IUserRequestBuilder>();
userRequestBuilder.Setup(x => x.Events).Returns(mockUserEventsCollectionRequestBuilder.Object);
Mock<IGraphServiceClient> graphServiceClient = new Mock<IGraphServiceClient>();
graphServiceClient.Setup(x => x.Users[It.IsAny<string>()]).Returns(userRequestBuilder.Object);
IApiClient apiClient = new ApiClient(graphServiceClient.Object);
// Act
Event newEvent = await apiClient.CreateAsync(eventItem);
// Assert
Assert.NotNull(newEvent);
Assert.AreEqual(@event.Id, newEvent.Id);
Assert.AreEqual(@event.Subject, newEvent.Subject);
}
}
Upvotes: 4