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
Try the below production code:
    public interface ISess
    {
    }

    public class UselessSess : ISess
    {
    }
    public interface IInstance
    {
        ISess Session { get; }
    }

    public class InstanceJob : IInstance
    {
        #region IInstance Members

        public ISess Session
        {
            get { throw new NotImplementedException(); }
        }

        #endregion
    }
    public class NHiber
    {
        private static IInstance instance;
        public static IInstance Instance
        {
            get
            {
                if (instance == null)
                    instance = new InstanceJob();
                return instance;
            }
        }
    }


And here's the test code:
        [Test]
        public void RunInstanceSingleton_Failed()
        {
            var uselessSess = new UselessSess();
            Isolate.WhenCalled(() => NHiber.Instance.Session)
                .WillReturn(uselessSess);
            Assert.IsInstanceOfType(typeof(InstanceJob), NHiber.Instance);
        }

        [Test]
        public void RunInstanceSingleton_Pass()
        {
            var uselessSess = new UselessSess();
            var instance = NHiber.Instance;
            Isolate.WhenCalled(() => instance.Session)
                .WillReturn(uselessSess);
            Assert.IsInstanceOfType(typeof(InstanceJob), NHiber.Instance);
        }


One test passes and another fails-- and both of them should pass.

I think this is a bug, and I think the reason behind this is that Typemock doesn't have the object in the chain properly, i.e., it should always try to return true object as far as it can, instead of assigning a mock value the moment it sees that it's inside the WhenCalled.
________
Genetically Modified Food
asked by nsoonhui (59.1k points)

1 Answer

0 votes
Hi Soon,

The reason that the test fails is because NHiber.Instance returns an interface.
When faking an interface the Isolator automatically creates a derived type.
The Isolator knows nothing about the internal implementation of NHiber.Instance so it can not guess whet derived type you want to use.

You can fix that by explicitly telling the Isolator to return the concrete type you want:

Isolate.WhenCalled(() => NHiber.Instance)
    .WillReturn(new InstanceJob());


Hope it helps.
answered by ohad (35.4k points)
...