Mr Perfect
Mr Perfect

Reputation: 685

How to Mock IOptions in NUnit?

Hi I am working in NUnit test case. In business layer I am using IOptions For example,

public MyBusinessLogic(IOptions<AuthenticationConfig> authenticationConfig, IOptions<ADFClient> AdfClient, IUnitOfWork unitOfWork)
{
_authenticationConfig = authenticationConfig.Value;
_ADFClient = AdfClient.Value;
UnitOfWork = unitOfWork ?? throw new ArgumentNullException(nameof(unitOfWork));
}

private AuthenticationConfig _authenticationConfig;

private ADFClient _ADFClient;

Then Below is my unit test

[TestFixture]
public class MyBusinessLogicTests
{
[SetUp]
        public void SetUp()
        {
            this.mockUnitOfWork = this.mockRepository.Create<IUnitOfWork>();
            this.mockAuthenticationConfig = new Mock<IOptions<AuthenticationConfig>>();
            this.mockADFClient = new Mock<IOptions<ADFClient>>();
        }

         private MockRepository mockRepository;
         private Mock<IUnitOfWork> mockUnitOfWork;
         private Mock<IOptions<AuthenticationConfig>> mockAuthenticationConfig;
         private Mock<IOptions<ADFClient>> mockADFClient;
         private ILogger<MyBusinessLogic> mockLogger;

        private MytBusinessLogic CreateMyBusinessLogic()
        {
            return new MyBusinessLogic(
                this.mockAuthenticationConfig,
                this.mockADFClient,
                this.mockUnitOfWork.Object
                );
        }
    }

In the CreateMyBusinessLogic, this.mockAuthenticationConfig giving me error saying that cannot convert from Moq.Mock<Microsoft.Extensions.Options.IOptions<Model.Entities.AuthenticationConfig>> to Microosft.Extensions.Options.IOptions<Models.Entities.AuthenticationConfig>

Can someone help me to fix this. Any help would be greatly appreciated. thank you

Upvotes: 0

Views: 294

Answers (1)

zaitsman
zaitsman

Reputation: 9519

Change the code like so:

private MytBusinessLogic CreateMyBusinessLogic()
        {
            return new MyBusinessLogic(
                this.mockAuthenticationConfig.Object,
                this.mockADFClient.Object,
                this.mockUnitOfWork.Object
                );
        }

With Moq the Mock<IType> is not the same as IType, you need to pass along the .Object property of it to access the mocked proxy.

Upvotes: 2

Related Questions