Search code examples
c#.netunit-testingmockingnmock2

Have NMock2 to create instance of a Type


I have this situation:

A method that expects a Type as one of its parameter; This Type must be a Type that implements an Interface in the project; I need to write tests for this method; I'm using NMock2;

Is there any way to get a Type from NMock2 so I can use it as a parameter of this method instead of create an implementation of this interface?

Thanks!


Solution

  • I prefer using Moq, but from what I can see, you should be able to retrieve a fake and use it like follows:

    var mocks = new Mockery();
    var someFakeType = mocks.NewMock<ISomeType>();
    
    // do whatever you need to setup this fake
    // such as setting expectations, stubing properties or methods, etc
    
    var someObject = new SomeObject();
    someObject.MethodUnderTest(someFakeType);