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
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
i ran into a problem when trying to use Isolator in the next example:

public interface ICookieManager
{
      ICookie<T> GetCookie<T>() where T : class, IChocolate, new();
}

public interface ICookie<T> where T : IChocolate
{}

public interface IChocolate
{}

public class MilkChocolate : IChocolate
{}

[TestMethod()]
public void MainTest()
{
      ICookiesManager cookiesManager = Isolate.Fake.Instance<ICookiesManager>();
      ICookie<MilkChocolate> cookie = Isolate.Fake.Instance<MilkChocolate>();
      Isolate.WhenCalled(() => cookiesManager.GetCookie<MilkChocolate>()).WillReturn(cookie);
}


the exception i get is "Unit Test Adapter threw exception: GenericArguments[0], 'T', on ' ICookie`1[T]' violates the constraint of type parameter 'T'.."

but as you can see, no constraint have been violated.

thank you.
asked by mod (5.8k points)

4 Answers

0 votes
Hi,

I managed to recreate the error message when running the example on our development environment - it seems like you found a bug :oops: .

We'll work to resolve this quickly and send you a working patch.

Thanks,
Doron,
Typemock Support
answered by doron (17.2k points)
0 votes
Looking further into this issue it seems like a problem Isolator has faking methods with return values depending on generic type constraints. I will keep updating on this issue's resolution and we'll work to fix this in an upcoming version.

Doron
Typemock Support
answered by doron (17.2k points)
0 votes
Hello,

We've encountered a similar situation with the exact same exception.
Do you have a progress with this bug? Maybe you can show us a way around?

Thanks
answered by benb (720 points)
0 votes
There is a simple workaround that will cause the test to pass, simply remove the constraint of ICookie.
public interface ICookieManager
{
    ICookie<T> GetCookie<T>() where T : class, IChocolate, new();
}

public interface ICookie<T>
{ }

public interface IChocolate
{ }

public class MilkChocolate : IChocolate
{ }

[TestMethod()]
public void MainTest()
{
    var cookiesManager = Isolate.Fake.Instance<ICookieManager>();
    var cookie = Isolate.Fake.Instance<ICookie<MilkChocolate>>();
    Isolate.WhenCalled(() => cookiesManager.GetCookie<MilkChocolate>()).WillReturn(cookie);
}


You can also use ReturnRecursiceFake to set the expectation on GetCookie method:
[TestMethod()]
public void MainTest()
{
    var cookiesManager = Isolate.Fake.Instance<ICookieManager>();
    Isolate.WhenCalled(() => cookiesManager.GetCookie<MilkChocolate>()).ReturnRecursiveFake();
}


Of course you will be informed when the issues will be resolved.

Dror Helper
Typemock Support
answered by dhelper (11.9k points)
...