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'm currently using version 6.0.8 of TypeMock Isolator.

The following code throws a NullReferenceException from the TypeMock framework:

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TypeMock.ArrangeActAssert;
using Microsoft.Office.Interop.Excel;

namespace TypeMockAndExcel
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        [Isolated]
        public void TestMethod1()
        {
            _Application fakeExcel = Isolate.Fake.Instance<Application>();
            Isolate.WhenCalled(() => fakeExcel.ShowStartupDialog).WillReturn(true);
            Isolate.WhenCalled(() => fakeExcel.ShowStartupDialog = true).WillReturn(false);  // Exception here
        }
    }
}


The stack trace is:

gh.a(Mock A_0, MethodInfo A_1)
gh.a(Mock A_0, MethodInfo A_1, TypeParams A_2)
aw.a(Mock A_0, Object A_1, MethodBase A_2, Object A_3, Object[] A_4)
gh.b(Object A_0, String A_1, String A_2, MethodBase A_3, Object[] A_4, Object A_5)
Mock0000Application.set_ShowStartupDialog(Boolean )
TypeMockAndExcel.UnitTest1.<>c__DisplayClass2.<TestMethod1>b__1() in c:userssextondocumentsisual studio 2010ProjectsTypeMockAndExcelTypeMockAndExcelUnitTest1.cs: line 20
TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Boolean A_5, Object[] A_6)
TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected, Boolean isInterceptedType)
TypeMockAndExcel.UnitTest1.TestMethod1() in c:userssextondocumentsisual studio 2010ProjectsTypeMockAndExcelTypeMockAndExcelUnitTest1.cs: line 0
asked by kevinms99 (4.4k points)

3 Answers

0 votes
Hi Kevin,

The correct error message for this error is:
Test method TypeMockAndExcel.UnitTest1.TestMethod1 threw exception:
TypeMock.TypeMockException:
*** When using WhenCalled for changing behavior of a property setter,
Instead of calling Isolate.WhenCalled(() => fake.ShowStartupDialog = false) wrap the call with curly brackets and update the completing statement.
For example: Isolate.WhenCalled(() => { fake.ShowStartupDialog = false; }).IgnoreCall()

The message was fixed in later versions.

After rewriting the statement to what the error message suggests :
{ fake.ShowStartupDialog = false; }


You will notice that it is impossible to set a return value on the setter of the property because it's void. It is possible to set expectation on property's return value as you did :
Isolate.WhenCalled(() => fakeExcel.ShowStartupDialog).WillReturn(true);

Or by using the auto-property feature of Isolator and set the expectation the following way:
fakeExcel.ShowStartupDialog = true;


You can find more info on properties here.
answered by alex (17k points)
0 votes
Alex--

Thanks for the information, but I still get the same behavior:

        [TestMethod]
        [Isolated]
        public void TestMethod1()
        {
            _Application fakeExcel = Isolate.Fake.Instance<Application>();
            Isolate.WhenCalled(() => fakeExcel.ShowStartupDialog).WillReturn(true);
            Isolate.WhenCalled(() => { fakeExcel.ShowStartupDialog = false; }).DoInstead((context) => { });
        }


still results in:

gh.a(Mock A_0, MethodInfo A_1)
gh.a(Mock A_0, MethodInfo A_1, TypeParams A_2)
aw.a(Mock A_0, Object A_1, MethodBase A_2, Object A_3, Object[] A_4)
gh.b(Object A_0, String A_1, String A_2, MethodBase A_3, Object[] A_4, Object A_5)
Mock0000Application.set_ShowStartupDialog(Boolean )
TypeMockAndExcel.UnitTest1.<>c__DisplayClass4.<TestMethod1>b__2() in C:UserssextonDocumentsVisual Studio 2010ProjectsTypeMockAndExcelTypeMockAndExcelUnitTest1.cs: line 20
TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Boolean A_5, Object[] A_6)
TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected, Boolean isInterceptedType)
TypeMockAndExcel.UnitTest1.TestMethod1() in C:UserssextonDocumentsVisual Studio 2010ProjectsTypeMockAndExcelTypeMockAndExcelUnitTest1.cs: line 0
answered by kevinms99 (4.4k points)
0 votes
Hi Kevin,

There is a possibility that this issue was solved in later versions.
Please install the version available at https://www.typemock.com and check if it works.

P.S

You can use:
Isolate.WhenCalled(() => { fakeExcel.prop = true; }).IgnoreCall();
Instead of:
Isolate.WhenCalled(() => { fakeExcel.prop = true; }).DoInstead(ctx => { });
answered by alex (17k points)
...