带有Ninject Mocking的Moq,Kernel.GetMock <t>不允许在同一个Mock对象上进行多次设置</t>

时间:2014-11-26 15:39:32

标签: unit-testing ninject moq

我正在为我们的网络应用程序构建单元测试。我从服务层开始,嘲笑存储库和服务。我们的应用程序使用Ninject IoC几乎所有东西......它整洁干净。我在服务的同一个模拟实例上使用第二个安装程序时遇到问题。基本上没有看到它,并且在调试时参数/返回值不像其他参数/​​返回值那样工作。有关详细信息,请参阅代码。

    private MoqMockingKernel Kernel; //Ninject Mocking
    private Mock<IClaimRepository> MoqClaimRepository{
        get
        {
            if (Kernel==null)
                throw new InstanceNotFoundException("MoqClaimRepository, MockingKernel has not been initialized.  [TestFixtureSetup] failed.");
            return Kernel.GetMock<IClaimRepository>();
        }
    }
    private Mock<IContactRepository> MoqContactRepository
    {
        get
        {
            if (Kernel == null)
                throw new InstanceNotFoundException("MoqContactRepository, MockingKernel has not been initialized.  [TestFixtureSetup] failed.");
            return Kernel.GetMock<IContactRepository>();
        }
    }
    private IClaimService MoqClaimService{
        get
        {
            if (Kernel == null)
                throw new InstanceNotFoundException("MoqClaimService, MockingKernel has not been initialized.  [TestFixtureSetup] failed.");
            return Kernel.Get<IClaimService>();  
        }
    }

    [SetUp]
    public void Init(){
        Kernel = new MoqMockingKernel();
        Kernel.Bind<IClaimService, IDataService>().To<ClaimService>();
    }





    [Test]
    public void the_Export_should_return_results(){
        //Arrange

        MoqClaimRepository.Setup(x => x.Search(It.IsAny<IClaimSearchCriteria>()))
            .Returns(ClaimObjectsSetup.ClaimSearchResultsSetup);
        MoqClaimRepository.Setup(x => x.GetExportData(It.IsAny<string[]>()))
            .Returns(ClaimObjectsSetup.ClaimExportDataResultsSetup);        //NOT SEEING THIS SETUP INSIDE THE SERVICE
        MoqContactRepository.Setup(x => x.Get(It.IsAny<ContactCriteria>()))
            .Returns(ClaimObjectsSetup.ClaimSearchContactResultsSetup);
        //Act
        MoqClaimService.Export(new ClaimSearchCriteria());
        //Assert
        MoqClaimRepository.VerifyAll();
    }

0 个答案:

没有答案