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
The example code for custom argument checking doesn't work on my system. Check() doesn't get called, and the test passes when it shouldn't. Here is the complete source:

using NUnit.Framework;
using TypeMock;

namespace Partners.Ccrx.Bridge.Connector.Tests
{
    public class TestedClass
    {
        public void myMethod( int a )
        {
        }
    }

    [TestFixture]
    public class TestFixture
    {
        public static bool Check( ParameterCheckerEventArgs args )
        {
            return (int)args.ArgumentValue > 4;
        }

        [Test]
        public void ParameterChecker()
        {
            // Mock our class 
            Mock mock = MockManager.Mock( typeof( TestedClass ) );
            // We will expect myMethod to be called once with a paramter that is > 4 
            mock.ExpectCall( "myMethod" ).Args( new ParameterCheckerEx( Check ) );
            // Lets just call myMethod with 6 (this should pass) 
            TestedClass t = new TestedClass();
            t.myMethod( 6 );
            // Lets just call myMethod with 3 (this should fail) 
            t.myMethod( 3 );
        }
    }
}
asked by Mark Wilden (640 points)

3 Answers

0 votes
Hi
The problem here is that only the first call to myMethod is mocked
so the argument checker is not called the second time.

To mock and check arguments on all calls Use:
mock.ExpectAlways( "myMethod").Args( new ParameterCheckerEx( Check ) );


To mock and check arguments only n times (2 times in this example) use:
mock.ExpectCall( "myMethod", 2 ).Args( new ParameterCheckerEx( Check ) );


Hope it helps.
answered by ohad (35.4k points)
0 votes
OK, that makes sense -- I should've realized that. Can I suggest you change the documentation accordingly?

///ark
answered by Mark Wilden (640 points)
0 votes
Hi Mark
Thanks for the input 8)
We'll see what we can do to make clearer.
answered by ohad (35.4k points)
...