Как имитировать IOptions в NUnit? - PullRequest
0 голосов
/ 07 августа 2020

Привет, я работаю в тестовом примере NUnit. На бизнес-уровне я использую IOptions. Например,

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;

Затем ниже мой модульный тест

[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
                );
        }
    }

В CreateMyBusinessLogi c this.mockAuthenticationConfig дает мне ошибку, говоря, что не может преобразовать из Moq.Mock<Microsoft.Extensions.Options.IOptions<Model.Entities.AuthenticationConfig>> to Microosft.Extensions.Options.IOptions<Models.Entities.AuthenticationConfig>

Может кто-нибудь поможет мне это исправить. Любая помощь будет принята с благодарностью. спасибо

1 Ответ

2 голосов
/ 07 августа 2020

Измените код следующим образом:

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

С Moq Mock<IType> не то же самое, что IType, вам нужно передать его свойство .Object, чтобы получить доступ к смоделированный прокси.

...