I am writing unit tests using xunit and moq for services in a servic fabric application.
I am passing the mocks like this
private static UsageDataQueueService CreateUsageDataQueueService(
Mock<IServiceBusClientFactory> serviceBusClientFactoryMock )
{
// Exception is being thrown at this line below
serviceBusClientFactoryMock ??= new Mock<IServiceBusClientFactory>();
return new UsageDataQueueService(serviceBusClientFactoryMock.Object);
}
Then test code is ( I have removed the asserts because it is unrelated):
[Fact]
public async Task QueueUsageData()
{
// arrange
Mock<IServiceBusClientFactory> serviceBusClientFactoryMock = new Mock<IServiceBusClientFactory>();
serviceBusClientFactoryMock
.Setup(factory => factory.CreateServiceBusClient(It.IsAny<string>()))
.Returns(It.IsAny<IServiceBusClient>());
// act
UsageDataQueueService usageDataQueueService = CreateUsageDataQueueService(serviceBusClientFactoryMock: serviceBusClientFactoryMock);
}
I am getting Null Reference Exception. Please help. Is this much context enough to answer the question or is more details needed?
CodePudding user response:
As pointed out by @Alexander Petrov in comments, problem lies in Returns(It.IsAny(). It must return either concrete instance or IServiceBusClient mock.