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, I receive System.InvalidCastException: Unable to cast object of type 'Mock0003IShellSettings' to type 'SampleProject.ShellMainForm' error when trying to mock 2 consecutive calls to UnityContainer.Resolve<T>() methods (at the second WhenCalled line).
I know that mocking IUnityContainer interface instead of UnityContainer class will fix the problem with Isolate.WhenCalled(), but this approach will prevent me from swapping next instance with a mocked one.

I am using: Unity 1.2; Isolator 5.3.5; VS 2008 SP1; Windows 7.

Sample code is:
using Microsoft.Practices.Unity;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TypeMock.ArrangeActAssert;

namespace SampleProject
{
    public class ShellMainForm { }

    public interface IShellSettings { }

    [TestClass]
    public class SampleTest
    {
        [TestMethod]
        [Isolated]
        public void GenericMethodsFail()
        {
            UnityContainer container = Isolate.Fake.Instance<UnityContainer>();
            Isolate.Swap.NextInstance<UnityContainer>().With(container);

            IShellSettings settings = Isolate.Fake.Instance<IShellSettings>();
            Isolate.WhenCalled(() => container.Resolve<IShellSettings>()).WillReturn(settings);
            ShellMainForm form = Isolate.Fake.Instance<ShellMainForm>();
            Isolate.WhenCalled(() => container.Resolve<ShellMainForm>()).WillReturn(form);
        }
    }
}
asked by dimonytch (1.2k points)

3 Answers

0 votes
Hi,

This is a known bug we have :twisted:
It happens with Unity, but it's more about dealing with overloaded generic methods.

We'll keep you updated when we get this resolved.

Thanks,
answered by gilz (14.5k points)
0 votes
Are there any updates to this problem yet?
This is a critical deal-breaker for any applications that want to use Unity for Dependency Injection which is more and more commonplace.

Any applications that make service calls likely have local versions of the service classes that are used when running the application during development, and dependency injection is a really common pattern for solving this problem in a clean way.

Any comments on the plans for getting this working? Also if you could shed any light on the underlying issues it would be kindly appreciated. This so far prevents us from adopting TypeMock as a feasible mocking framework for our organization, which is sad because there are certainly a lot of upsides we were looking forward to.


Another variation that doesn't work:
ILoginService loginService = Isolate.Fake.Instance<ILoginService>();
Container.RegisterInstance( loginService );
Isolate.WhenCalled( () => loginService.Authenticate( validCredentials )).WillReturn( true );

after resolving the mocked instance the matchers that were set up do not match:
ILoginService loginService = Container.Resolve<ILoginService>()
bool thisShouldReturnTrueButReturnsFalse = loginService.Authenticate( validCredentials );
answered by marchy (140 points)
0 votes
Hi,

We plan to fix this bug in the near future, I can't give yet exact time but it's a high priority bug.

Are you using in the production code in the concrete UnityContainer or IUnityContainer? If you use the interface, it's possibly that the issue will be solved using fakes of IUnityContainer;

This bug is caused in specific cases where method is overloaded with generic overload, with different return types on which generic constraint defined.

Can you please post the code of ILoginService? I would like to reproduce it here and verify it's caused by the same reason.

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