简体   繁体   English

使用Moq或任何其他模拟工厂从模拟存储库中检索模拟

[英]Retrieve a mock from a mock repository with Moq or any other mock factory

How can I retrieve a mock object from a mock repository using moq? 如何使用moq从模拟存储库中检索模拟对象?

I have an application (mvc2) that wasn't really designed from the first time to support test units, but I am trying to adapt it. 我有一个不是第一次设计的应用程序(mvc2),它不支持测试单元,但我正在尝试对其进行调整。 I have a service locator that creates the desired types. 我有一个创建所需类型的服务定位器。

basically what I want to do using a mock framework is replacing the variables that represent services in a method with mock objects, then I want to retrieve that mock objects and set them up. 基本上,我想使用模拟框架执行的操作是用模拟对象替换表示方法中服务的变量,然后我要检索该模拟对象并进行设置。

So: 所以:

private ApprovalLevel GetApprovalLevel(Fusion Fusion)
{
    var ScopeofService=ServiceLocator.GetInstance<IScopeofService>();
    var programs=ScopeofService.GetPrograms();
    ...    
} 

In my test units I set the ServiceLocator to use a mock kernel that will mock up my IScopeofService this works fine. 在测试单元中,我将ServiceLocator设置为使用模拟内核,该内核将模拟我的IScopeofService,这可以正常工作。 The mock kernel uses a mock repository sent as param MockRepository Moq, that will bind and create the desired mock for a desired interface. 模拟内核使用作为参数MockRepository Moq发送的模拟存储库,它将绑定并为所需的接口创建所需的模拟。

Now I want to retrieve that mock in my test to setup a fake value to be returned for the GetPrograms() but I MockRepository doesn't have a get mock just methods to create new mocks. 现在,我想在测试中检索该模拟,以设置要为GetPrograms()返回的假值,但我的MockRepository没有一个仅用于创建新模拟的get模拟方法。

Still he somehow keeps all the references to mocks because it has VerifyAll method. 他仍然以某种方式保留了对模拟的所有引用,因为它具有VerifyAll方法。

Update 更新资料

public MockNinjectControllerFactory(Moq.MockRepository mockRepository)
{
    Kernel = new StandardKernel(new MockNinjectServices(mockRepository));
}

public class MockNinjectServices : NinjectModule
{
    private readonly MockRepository _mockRepository;
    public MockNinjectServices(MockRepository _mockRepository)
    {
        this._mockRepository = _mockRepository;
    }

    // TODO MARIAN: Resolver should return actualy mock objects also to be able to set them.
    public void BindToConstant<T>() where T : class
    {
        Bind<T>().ToConstant(_mockRepository.Create<T>().Object).InSingletonScope();
    }

    public void BindMock<T>() where T : class
    {
        Bind<Mock<T>>().To(typeof(Mock<T>)).InSingletonScope();
    }

    public override void Load()
    {
        BindToConstant<IPaymentInvoiceRep>();
        BindToConstant<IPaymentInvoiceBL>();
    }
}

And the test itself: 测试本身:

[TestFixture]
public class DemoControllerFixture
{
    readonly MockRepository _mockRepository = new MockRepository(MockBehavior.Default);

    [Test]
    public void Should_MyMethod()
    {           
        EvedServiceLocator.Default = new MockNinjectControllerFactory(_mockRepository);
        var sut=new DemoController();

        // now if my sut has something that will resolve as mock created by my mockRepository I would like to retrieve that mock and set up something on it.            
    }
}

Use Moq.Get() to get the mock object from a concrete reference. 使用Moq.Get()从具体引用中获取模拟对象。 In code: 在代码中:

[Test]
public void Should_MyMethod()
{           
    EventServiceLocator.Default = new MockNinjectControllerFactory(_mockRepository);
    var sut=new DemoController();

    var scopeService EventServiceLocator.Default.GetInstance<IScopeofService>();
    var mockScopeService = Mock.Get(scopeService);
    mockScopeService.Setup(p => p.GetPrograms()).Returns(/* TODO */); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM