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 following code
    public class DerivedController:System.Web.Mvc.Controller
    {
        
    }
    [TestFixture, ClearMocks]
    public class ControllerTest
    {
        [Test, Isolated]
        public void FirstController()
        {
            var controller = Isolate.Fake.Instance<DerivedController>();
            Isolate.WhenCalled(()=>controller.Session).ReturnRecursiveFake();
            Isolate.WhenCalled(() => controller.Session["a"])
                .WillReturn("a");

            Assert.IsNull(controller.Session["b"]);

        }
    }


There is this exception:
failed: TypeMock.TypeMockException : 
*** Unexpected Call to System.Web.HttpSessionStateBase.get_Item()
   at b3.b(Object A_0, String A_1, String A_2, MethodBase A_3, Object[] A_4, Object A_5)
   at System.Web.HttpSessionStateBase.get_Item(String name)
   C:\development\Esteem7 .Net2\ELockRecordWeb\Source\ELockRecordWeb\ELockRecordWebTest\Controller\ControllerTest.cs(27,0): at ELockRecordWebTest.Controller.ControllerTest.FirstController()
   at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
   at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)


But if you comment this line:
   Isolate.WhenCalled(() => controller.Session["a"])
                .WillReturn("a");


Then the test can pass.

I suspect that the above line disturbs the states in Isolator.

This is quite an urgent case for me because it lies in my production code. Do have a check
________
Babi Mac Breakaway
asked by nsoonhui (59.1k points)

2 Answers

0 votes
Hi Soon Hui,

We'll take a look at this and get back to you quickly.

In the meantime, here is a tip: You can lose the ReturnRecursiveFakes, because it is already like this, it's a property of a fake object.

Also, Remove the ClearMocks attribute. Use Isolated instead, one on the class is enough, instead than on all tests.

Thanks,
answered by gilz (14.5k points)
0 votes
Hi Soon Hui,


Also, Remove the ClearMocks attribute. Use Isolated instead, one on the class is enough, instead than on all tests.

Thanks,


Thanks, maybe you can consider obsolete the ClearMocks attribute since it is no longer required.
________
Honda Z50J
answered by nsoonhui (59.1k points)
...