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
Repeat with a chained statement does not seem to be working properly. A test with this recorded fails on the second access:

using(RecordExpectations recorder = RecorderManager.StartRecording()){
recorder.ExpectAndReturn(promoCode.Promotion.IsActive, true).Repeat(2);      
}


but yet this works

using(RecordExpectations recorder = RecorderManager.StartRecording()){
recorder.ExpectAndReturn(promoCode.Promotion.IsActive, true);
recorder.ExpectAndReturn(promoCode.Promotion.IsActive, true);
}


this is the error that the first code block generates when the 2nd expectation is hit

TestCase 'GameFly.Retail.UnitTests.Domain.Marketing.RentalPromoCodeFixture.MaxedOutIfToManyRedeemed'
failed: System.NullReferenceException: Object reference not set to an instance of an object.
   at TypeMock.MethodDecorator.b()
   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)
   C:\Data\Projects\GameFly\Retail\MainLine\Src\GameFly.Retail.UnitTests\Domain\Marketing\RentalPromoCodeFixture.cs(52,0): at GameFly.Retail.UnitTests.Domain.Marketing.RentalPromoCodeFixture.MaxedOutIfToManyRedeemed()


Let me know if I can provide any more info.
asked by juice_johnson (8.8k points)

3 Answers

0 votes
Hi,

Can you please specify what version are you using.
Several issues related to the "Repeat" behaviour were solved in the latest update (4.0.2). Your problem might be one of those.

Also if possible please put the entire test code. that will help us investigate this.
answered by lior (13.2k points)
0 votes
Hi,
As Lior said, it is quite hard to know what the issue is without the test code.

You might want to use the Tracer to see the expectations and calls.

In any case this issue might have to do with understanding the Repeat and Chained Statements.
1. When using Repeat/Return TypeMock refers to the LAST statement in the chain.
2. When using Check with Custom Argument, TypeMock refers to ALL the statements in the chain.

Currently to repeat all the statements in the chain use:
recorder.DefaultBehavior.Repeat(2);
recorder.ExpectAndReturn(promoCode.Promotion.IsActive, true);

this will tell TypeMock to repeat all the methods in the chain.

Do you think that we should change the behavior of Repeat? or to add a RepeatLastMethod()?
answered by scott (32k points)
0 votes
Hi Scott,
I tried the latest version and that did not resolve the problem. I believe you are correct when you say that I may have misunderstood the usage of repeat with a chained statement. I was thinking that repeat would mock all the calls of the chained statement multiple times. Your sample resolved the problem and gave me the expected results.

Repeating the last statement and not the whole chain is not what I expected. Personally I don't see the value of just repeating the last method of a chained statement. If I am using a chained statement, it is because I don't wont to go through the hassle of creating the whole object hierarchy. Maybe there are some use cases but I cant think of any right now for just repeating the last statement in the chain.
answered by juice_johnson (8.8k points)
...