Dan
Dan

Reputation: 921

FakeItEasy: Fake a call to a generic method without specifying the type

I have a type that relies on an external component that executes queries through a generic method. Here's a simplified version of what's in play:

public class UnitUnderTest
{
    private IQueryEngine _engine;

    public UnitUnderTest(IQueryEngine engine)
    {
        _engine = engine;
    }

    public OutputType DoSomething() 
    {
         _engine.Query<ExternalType>(...);
         _engine.Query<InternalType>(...);
    }
}

public interface IQueryEngine
{
    IEnumerable<T> Query<T>(string conditions);
}

The DoSomething method I'm trying to test calls IQueryEngine twice.

The first time it calls it with a type I can access from my test so I have no problem using A.CallTo to mock it.

But then it calls a second time with a type argument that I can't access (it's an internal type).

Right now my test is bombing because DoSomething is not expecting back a null from the call to _engine.Query, but I cannot construct the proper type of object to return.

How can I mock out this call?

Upvotes: 2

Views: 829

Answers (1)

Daniel A. White
Daniel A. White

Reputation: 190945

This sounds like a perfect use of the InternalsVisibleTo attribute. Just add an instance to your assembly with the internal type with your test assembly as the value.

Upvotes: 1

Related Questions