chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
Hi,

The following test throws null argument exception exception:

[TestMethod]
public void FakingUnityContainerThrowsNullExceptionOnWhenCalled()
{
// Arrange
IUnityContainer unityContainer =
Isolate.Fake.Instance<IUnityContainer>();

Isolate.WhenCalled(() =>
unityContainer.Resolve(typeof(string))).WillReturn(null);

// Act

// Assert
}

Is this a bug?
asked by mod (5.8k points)

3 Answers

0 votes
Yes it is a bug. Currently we have a known issue when faking a method that returns a compound generic (IEnumerable<T>).

When we resolve this issue in the neat future I will let you know.
answered by dhelper (11.9k points)
0 votes
Hi,

I think this Reslove<T>(type) returns a single object of type <T> and not a compound generic. Does it mean you have also problems with this kind of methods?
answered by benb (720 points)
0 votes
Hi,

It is possible to fake the behavior of a generic method.

For example:
public class GenericDefaultValueResolver
{
    public T ReturnsDefault<T>()
    {
        return default(T);
    }
}

[TestClass]
public class GenericDefaultValueResolverTests
{
    [TestMethod]
    public void GenericMethodTest()
    {
        var genericResolver = Isolate.Fake.Instance<GenericDefaultValueResolver>();
        Isolate.WhenCalled(() => genericResolver.ReturnsDefault<string>()).WillReturn("1");
        Isolate.WhenCalled(() => genericResolver.ReturnsDefault<int>()).WillReturn(1);

        var intValue = genericResolver.ReturnsDefault<int>();
        var stringValue = genericResolver.ReturnsDefault<string>();
        
        Assert.AreEqual(1, intValue);
        Assert.AreEqual("1", stringValue);
    }
}


As to the issue with Unity - it's a case which requires further invastigation and will be resolved in the near future.

Best Regards,
Elisha
Typemock Support Team
answered by Elisha (12k points)
...