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

I have been trying to create some unit tests for the following code:


void SecurityInitialized(PassedObject pObj) {
_dispatcher.BeginInvoke((CrossAppDomainDelegate) delegate() {

            try {
               pObj.DoSomething();
            }
            catch (Exception exception) {

            }
         });
      }


_dispatcher is an object of System.Windows.Forms.Form class. Is there a way to mock the pObj object from the inside of the BeginInvoke?

I have tried:

Mock pObjMock = MockManager.MockObject(typeof(PassedObject));
pObjMock.ExpectCall("DoSomething");

and calling the SecurityInitlialized method

but it does not work. Typemock says that:

Not all expectations where called:
Method PassedObject.DoSomething has 1 more expected calls.
asked by tbarto (1.1k points)

1 Answer

0 votes
Hi
Did you passed the mocked instance to the SecurityInitialized method?
i.e
ClassUnderTest classUnderTest = new ClassUnderTest();
classUnderTest.SecurityInitialized(pObjMock.Object as PassedObject);


:arrow: Remember when you create a MockObject the Isolator create an instance and mock it. So the expectations are made on the instance in MockObject.Object and not on future object.

Hope it helps.
If it still doesn't work please post here the full test code and we'll try to help you.
answered by ohad (35.4k points)
...