user926958
user926958

Reputation: 9565

Mock an Interface that will be instantiate later?

Here is my code below. I am using the Moq library. Instead of letting Moq create an instance for me. I need to flat into a type to pass to someone to create instance later. How do I do that?

var mock = new Mock<IFoo>();
mock.Setup(foo => foo.Bar()).Returns(true);
// Test the actual method
DoSomething(mockedIStateProvider.Object.GetType());


// Not my code, it's a 3rd party library, not allowed to change
public void DoSomething(Type type)
{
    Activator.CreateInstance(type);
    ...
    ...
}

Upvotes: 2

Views: 115

Answers (1)

Mike Parkhill
Mike Parkhill

Reputation: 5551

You can't with Moq. The Activator will always create a new instance which you won't have access to so you won't be able to setup the mocking behaviour you want.

That being said, you can probably get the behaviour you want by writing your own stub implementation that will always do what you want.

Upvotes: 1

Related Questions