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

suppose that i want to check an argument against a method with a signature like

bool IsIn(param object[] list)

how can i do it

other libraries have build in a number of argument constrains do u have such support

by the way the type mock looks/works beautifull
asked by tolisss (28.8k points)

3 Answers

0 votes
Hi,
suppose that i want to check an argument against a method with a signature like

bool IsIn(param object[] list)


There are 3 ways to check the arguments of this method signiture.
First, lets remember that the CLR sees the param object[] argument as object[], so we have to check that the argument object[] fits our expectations.

Here are the different methods:

1. Use Args, TypeMock.NET will automatically validate all items of an array. So our code might look like this.
[Test]
public void testIsIn()
{
  Mock mock = MockManager.Mock(typeof(TestedClass));
  TestedClass t = new TestedClass();
  object expectedParameters = new object[] {3,"tt",false,MockManager.Any};
  mock.ExpectAndReturn("IsIn",false).Args(expectedParameters);
  Assert.AreEqual(false,t.IsIn(3,"tt",false,"SFD");
}

:idea: Tip: If you have more arguments before the params, you don't have to create the expectedParameters variable, but can use new object[]{} in the Args method.
:idea: Tip: You can pass a ParameterChecker delegate, to have your own validator. This can be done as also per array item. In this example we are using MockManager.Any which is actually an implementation of this delegate.

2. Use the ParameterChecker
public static bool CheckParams(object parameter)
{
  object[] parameters = (object[])parameter;
  bool ok = (3==(int)parameters[0]) &&
    ("tt"==(string)parameters[1]) &&
    (false==(bool)parameters[2]);
  return ok;
}
[Test]
public void testIsIn()
{
  Mock mock = MockManager.Mock(typeof(TestedClass));
  TestedClass t = new TestedClass();
  mock.ExpectAndReturn("IsIn",false).Args(new ParameterChecker(CheckParams))
  Assert.AreEqual(false,t.IsIn(3,"tt",false,"SFD");
}

Here we are are checking the arguments, in the ParameterChecker delegate


3. Use the DynamicReturnValues.
public object ParamsRetValue(object[] pars,object context) 
{
  // first parameter is an object array
  object[] parameters = (object[])pars[0];
  Assert.AreEqual(3,(int)parameters[0]);
  Assert.AreEqual("tt",(string)parameters[1]);
  Assert.AreEqual(false,(bool)parameters[2]);
  return false;
}
[Test]
public void testIsIn()
{
  Mock mock = MockManager.Mock(typeof(TestedClass));
  TestedClass t = new TestedClass();
  mock.ExpectAndReturn("IsIn",new DynamicReturnValue(ParamsRetValue));
  Assert.AreEqual(false,t.IsIn(3,"tt",false,"SFD");
}


Here we are are checking the arguments, in the DynamicReturnValue delegate

other libraries have build in a number of argument constrains do u have such support

This is true, but instead of creating different constraints that developers will have to learn, we created a flexible interface that any checker can be implemented. We distribute TypeMock with one checker, the MockManager,Any. Of course if you would like to see more checkers please post a request. We will take your ideas seriously.

by the way the type mock looks/works beautifull


Thanks alot.
answered by scott (32k points)
0 votes
Just as a reminder, when using the DynamicReturnValues, you can continue with the normal execution of the method by returning MockManager.CONTINUE_WITH_METHOD (This is called verify mode)

public object ParamsRetValue(object[] pars,object context) 
{
  // first parameter is an object array
  object[] parameters = (object[])pars[0];
  Assert.AreEqual(3,(int)parameters[0]);
  Assert.AreEqual("tt",(string)parameters[1]);
  Assert.AreEqual(false,(bool)parameters[2]);
  return MockManager.CONTINUE_WITH_METHOD;
}

The above code will check the arguments and if they are ok, the original method will be run.
answered by scott (32k points)
0 votes
Hi,
This feature has been added in version 2.1
answered by richard (3.9k points)
...