Additional stub options in RhinoMock

I want to stub a function that receives 2 boolean parameters. The first is required and the second is optional. If I try to send Arg.Is.Anything to the first, but without information for the second, I get an error:

System.InvalidOperationException: When using Arg, all arguments must be defined with Arg.Is, Arg.Text, Arg.List, Arg.Ref, or Arg.Out. 2 arguments expected, 1 specified.

Here's an example of my stub class:

   public interface IOptionalParameterTester
   {
      bool IsValid(bool mustCheck, bool checkInDatabase = true);
   }

   public class OptionalParameterTester : IOptionalParameterTester
   {
      public bool IsValid(bool mustCheck, bool checkInDatabase = true)
      {
         if (checkInDatabase)
            return true;
         else
            return false;
      } 
   }

      

And here is a sample test:

  [Test]
  public void ValidateProducerTest()
  {
     IOptionalParameterTester optionalParameter = MockRepository.GenerateStub<IOptionalParameterTester>();

     optionalParameter.Stub(x => x.IsValid(Arg<bool>.Is.Anything)).Return(true);
  }

      

In this case, if I want the test to pass without errors, I have to define the same information as for the first one (Arg.Is.Anything), or a specific boolean value such as true or false.

If I set something other than Arg.Is.Anything for the first parameter, I don't have any error.

This is mistake?

Can I tweak a parameter in RhinoMock so I don't need to define a value for each optional parameter?

If there is no customization, is there something better to handle this case (best practice, pattern, etc.)?

Thank.

+3


source to share


3 answers


I think I understand what you are trying to do, but since it seems to be a limitation of Rhino Mocks (we can get to this conclusion from the error message you receive) I would suggest changing your testing strategy.

Try the following:

[Test]
  public void ValidateProducerTest()
  {
     bool anyBooleanValue = true;
     IOptionalParameterTester optionalParameter = MockRepository.GenerateStub<IOptionalParameterTester>();

     optionalParameter.Stub(x => x.IsValid(anyBooleanValue)).Return(true);
  }

      



I know that in your test, you want Rhino Mocks to ignore the first parameter and that it takes the optional second, but depending on the logic you want to test, just copy the first parameter and Rhino Mocks shouldn't complain.

As long as your test clearly states that the first parameter value is irrelevant, your test is valid and readable.

+1


source


I just had a similar problem, try



optionalParameter.Stub(x => x.IsValid()).IgnoreArguments().Return(true);

0


source


This is a pretty old question, but I landed on this page because I was having issues with AssertWasCalled and additional options.

In the end, I solved my problem using the following syntax found on this page: http://petermorlion.blogspot.com/2012/11/rhinomock-assertwascalled-vs.html .

string expectedMessage = "RhinoMocks baby!";
string actualMessage = "RhinoMocks dude!";
var fooMock = MockRepository.GenerateMock<ifoo>();
fooMock.Bar(actualMessage);

var calls = fooMock.GetArgumentsForCallsMadeOn(x => x.Bar(string.Empty), o => o.IgnoreArguments());
Assert.AreEqual(1, calls.Count);
var arguments = calls[0];
Assert.AreEqual(expectedMessage, arguments[0]);

      

0


source







All Articles