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
Mocking a chain object of the struct type will throw cannot use mockall exception.


Here's the production code
    public struct SpecialValue
    {
        public double X
        { get; set; }
        public double Y
        { get; set; }
       
    }
    public class Typemockclass
    {
        public SpecialValue pt1
        { get; private set; }
        public SpecialValue pt2
        { get; private set; }

    }


Here's the test code
        [Test, Isolated]
        public void  testSpecialValue()
        {
            Typemockclass mock = Isolate.Fake.Instance<Typemockclass>(Members.ReturnRecursiveFakes);
            Isolate.WhenCalled(() => mock.pt1.X).WillReturn(30);
            Isolate.WhenCalled(() => mock.pt2.X).WillReturn(60);
            
        }


Here's the exception:
TestCase 'TypeMockTest.testSpecialValue'
failed: TypeMock.TypeMockException : 
*** Cannot use MockAll twice for type SpecialValue
   at ds.a(Constructor A_0, Boolean A_1)
   at TypeMock.MockManager.MockAll(Type type, Constructor mockConstructors)
   at TypeMock.MockManager.Mock(Type type, Constructor mockConstructors)
   at TypeMock.MockManager.b(Object A_0, String A_1)
   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)
   at SpecialValue.get_X()


But let's say if the test is
        [Test, Isolated]
        public void  testSpecialValue()
        {
            Typemockclass mock = Isolate.Fake.Instance<Typemockclass>(Members.ReturnRecursiveFakes);
            Isolate.WhenCalled(() => mock.pt1.X).WillReturn(30);
          
            
        }


Then there is no error at all.
________
Weed vaporizers
asked by nsoonhui (59.1k points)

3 Answers

0 votes
Hi Soon Hui,

Struct support in Isolator is different than handling classes. The underlying reason is that structs are value types and don't really have instances in the sense classes do. Support is provided through the MockAll mechanism, which means all structs of the the same type will have the same faked behavior - the last behavior that was set will apply. This means that the test you are trying to write will probably not act as you expect by returning 60 for both pt1 and pt2. You can find more info here: https://www.typemock.com/community/viewt ... truct#2756

The issue you are experiencing is a mix of the way structs are handled (as described above), and a bug with the way they are handled in AAA - when this is fixed you will not get the exception, but the behavior above will apply (meaning you will get a return value of 60 from pt1 and pt2). I will let you know when the exception is handled properly.

Thanks,
Doron
Typemock Support
answered by doron (17.2k points)
0 votes
Hi Soon Hui,

when this is fixed you will not get the exception, but the behavior above will apply (meaning you will get a return value of 60 from pt1 and pt2). I will let you know when the exception is handled properly.

Thanks,
Doron
Typemock Support


Is there any plans to make sure that the mocking for struct is handled properly ( meaning TypeMock will return 30 for pt1.X and 60 for Pt2.X)?

OTOH, if I run the test
 [Test, Isolated] 
        public void  testSpecialValue() 
        { 
            Typemockclass mock = Isolate.Fake.Instance<Typemockclass>(Members.ReturnRecursiveFakes); 
            Isolate.WhenCalled(() => mock.pt1).WillReturn(new SpecialValue( 30,0)); 
            Isolate.WhenCalled(() => mock.pt2).WillReturn(new SpecialValue( 60,0)); 
          
            
        } 


It seems that the behavior is expected, namely mock.pt1 will return SpecialValue{X=30, Y=0} and mock.pt2 will return SpecialValue{X=60, Y=0}. But I am not sure on this, may need to get back to the office and check :)
________
Brunette xxx
answered by nsoonhui (59.1k points)
0 votes
This seems like a good workaround, and while I haven't checked it out yet I'm sorry I didn't suggest it myself :) Let us know if it works.
Doron
answered by doron (17.2k points)
...