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
Is it possible to setup an expectation on a MockObject after you have already used the Object property.

For example

MockObject myMockObject = MockManager.MockObject(typeOf(CoolObject));

this.SetCoolObject(myMockObject.Object as CoolObject);

myMockObject.ExpectAndReturn("GetCoolState", CoolState.ReallyCool);


After this should a call on the object execute the MockExpectation?

If so, it doesnt seem to be working.
asked by jnapier (9.6k points)

7 Answers

0 votes
Hi,
I have tried this with version 3.0.2 and it works,
What version are you using?
What is the error that you are receiving?
answered by scott (32k points)
0 votes
I am using 3.0.2.1.

I am not getting an error, but the method is not getting mocked. I know this because the method I have mocked is hitting the database and I am getting a VerifyException for the mocked object.


I cant use trace because trace is not working with the version I have.
answered by jnapier (9.6k points)
0 votes
What Verify exception are you getting?
answered by scott (32k points)
0 votes
RemoteNet.Northrop.IbisWork.UnitTests.Domain.Model.ProcessContextFixture.CannotDeleteIfHasProcedures : TypeMock.VerifyException :
TypeMock Verification: Method RemoteNet.Northrop.IbisWork.Domain.Model.Process.HasProcedures() has 1 more expected calls

This exception doesnt relate to the example I gave in the originating post because I wanted to simplify things but the concept is still the same.
answered by jnapier (9.6k points)
0 votes
Hi,
This cannot be the correct message, as MockObjects are Strict and should fail once you call an unmocked method.
Are you sure that your small example fails? Can you send an example that fails
answered by scott (32k points)
0 votes
I tried creating a sample and could not reproduce the problem either, so I started looking at how my code was different than the sample I created. It turns out that a previous bug had re-inserted itself into the code, possibly because of a rollback, but MockManager.Verify was getting called on test teardown as opposed to fixture teardown. This was causing the tests to get screwed up. So this problem has been resolved, it was our fault.

However, the test sample that I created did proove that mock objects are not operating strictly as you have stated in your last post.

My sample creates a Mock object and then I can call methods on that MockObject that do not have an expectation set. I can send the sample to you if this is not the expecte behavior.

Sorry for wasting your time on the other problem.
answered by jnapier (9.6k points)
0 votes
Hi,
There are actually two kinds of MockObjects.
1. From abstract and interfaces - these are always Strict
2. From other classes - in this case MockObject is just a shortcut for setting a mock and then creating the object. In this case the MockObject is not Strict

The reason for this is that interface methods just dont exist - so there is nothing to call, while other cases there is actual code to call.
answered by scott (32k points)
...