2

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?

Dan
  • 901
  • 11
  • 25
  • why don't you add the `InternalsVisibleTo` attribute? – Daniel A. White May 06 '16 at 15:34
  • Ideally I would have rather not done it this way, but since I did have control over the library where `InternalType` is defined, this is the solution I went with. If you turn this into an answer, I'll be happy to mark it as the correct one. – Dan May 10 '16 at 12:51

1 Answers1

1

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.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445