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
The assertion in the below test fails, because the expected call isn't mocked:

TestCase 'Test.TestGenericMock'
failed: TypeMock.VerifyException :
TypeMock Verification: Method OtherClass`1.GetInstance() has 1 more expected calls
at TypeMock.MockManager.Verify()
Test.cs(305,0): at Test.TestGenericMock()

Is there a way to do this?

Thanks! :)

    
[TestFixture]
    public class Test
    {
        [Test]
        public void TestGenericMock()
        {
            using (RecordExpectations recorder = RecorderManager.StartRecording())
            {
                OtherClass<string> mockedInstance = RecorderManager.CreateMockedObject<OtherClass<string>>();
                recorder.ExpectAndReturn(OtherClass<string>.GetInstance(), mockedInstance);
            }

            string data = TestedClass.GetData();
            MockManager.Verify();
            Assert.IsNull(data);
        }
    }

    public class TestedClass
    {
        public static string GetData()
        {
            return OtherClass<string>.GetInstance().Data;
        }
    }

    public class OtherClass<T>
    {
        private string data = null;
        public string Data 
        { 
            get { return data; }
            set { data = value; }
        }

        public static OtherClass<T> GetInstance()
        {
            OtherClass<T> instance = new OtherClass<T>();
            instance.Data = "i was not mocked";
            return instance;
        }
    }
asked by marcus (1.1k points)

1 Answer

0 votes
Hi Marcus,

It seems that you have uncovered a defect in the generic handling mechanism.
for now try writing this test using the reflective API:
[TestMethod]
public void genericUsingReflective()
{
    MockObject mock = MockManager.MockObject<OtherClass<string>>(Constructor.NotMocked);
    mock.ExpectAndReturn("GetInstance", mock.Object as OtherClass<string>);

    string data = TestedClass.GetData();
    MockManager.Verify();
    Assert.IsNull(data);
}


We will fix this defect and send you an update.
answered by lior (13.2k points)
...