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
Hi,

I'm trying to create a mock object of a struct that has methods on it. I am not sure if this is supported by typemock, but there are cases where it might be needed. Below is a test case that reproduces the problem. Any work arounds/fixes are appreciated.

Thanks,
- Wael

Note: We are using a struct instead of a class because we want the objects to be created on the stack (instead of the heap) for performance reasons...

Unit Under Test:
public struct A
{
    private long a;
    private long b;
    public long getA()
    {
        return a;
    }
} // struct A


Test Code:
MockObject mockA = MockManager.MockObject(typeof(A));


Error Message:
Test method IndexedObjectUnitTest.IndexedObjectTest.GTest threw exception: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index.


Error Stack Trace:
at System.Collections.ArrayList.get_Item(Int32 index)
at ar.a()
at TypeMock.MockManager.MockObject(Type type, Constructor mockConstructors, Object[] args)
at TypeMock.MockManager.MockObject(Type type, Object[] args)
at IndexedObjectUnitTest.IndexedObjectTest.GTest()
asked by hemdanw (1.7k points)

1 Answer

0 votes
Hi
The problem is that you can not create an instance of a struct.
Instances are created on the heap but not on the stack.
Never the less:
1. the ArgumentOutOfRangeException is a bug. We should give the poor user a polite message. :(
2. You can still use MockAll on structs.
3. We will try to add a this feature (Create Mock object of a structure) to TypeMock.
answered by ohad (35.4k points)
...