Arg<T>.Ref() in combination with Throw() not working as expected

40 views
Skip to first unread message

Sebastian Edelmeier

unread,
May 2, 2013, 2:24:50 AM5/2/13
to rhino...@googlegroups.com
Hi,

I've had an issue with the following test case, written against Rhino 3.6:

Method Code:

public class ClassUnderTest
{
   public string Arg0{get;set;}
   public string Arg1{get;set;}
   public IValidator Validator {get;set;}

   public bool Validate()
   {
      string proposal = string.Empty;
      try
      {
         if (Validator.IsValid(Arg0, Arg1, ref proposal)) return true;
      }
      catch (ValidationException ex)
      {
         if (!string.IsNullOrEmpty(proposal))
         {
            // I want to test this section of code
         }
      }
      return false;
   }                   
}

Test Code:

[TestMethod]
public void Test_Validate_ValidatorProposes_ReturnsTrue()
{
    string arg0 = "123456789";
    string arg1 = "201208150030551ABC";
    string prop = "123456";

    ClassUnderTest testInstance = new ClassUnderTest();
    testInstance.Arg0 = arg0;
    testInstance.Arg1 = arg1;

    IValidator validatorStub = MockRepository.GenerateStub<IValidator>();
    validatorStub.Stub(x => x.IsValid(Arg<string>.Is.Equal(arg0),
                                      Arg<string>.Is.Equal(arg1),
                                      ref Arg<string>.Ref(Is.Anything(), prop).Dummy))
                 .Throw(new ValidationException(string.Empty));
    testInstance.Validator = validatorStub;

    bool actual = testInstance.Validate();

    Assert.IsFalse(actual);
}
Actually, the validatorStub will throw the expected exception but not set the ref argument.

A User on StackOverflow, where I have initially posted this issue, hinted me to an issue that is based on the Castle DynamicProxy release that Rhino is working with that describes that there is a problem with exactly this combination (ref arguments and exception handling). Since that was two years ago, I wonder if there's a chance of getting a Rhino build that uses a later version of DynamicProxy, one where this is already solved.

Thanks in advance!

haifisch

unread,
May 2, 2013, 4:42:25 AM5/2/13
to rhino...@googlegroups.com
Hi Sebastian,

yould try the releases from 'my' branch (https://github.com/alaendle/rhino-mocks) - at least the test you've provided succeeds against the source code.

Br,

Andreas

Sebastian Edelmeier

unread,
May 2, 2013, 5:13:54 AM5/2/13
to rhino...@googlegroups.com
Hi Andreas, 

This test will - the way it is - succeed, since the method defaults to the expected value. Could you please replace the followin lines and run the test again:

1) change 
// I want to test this section of code
to
return true;

2)  change 
Assert.IsFalse(actual);
to
Assert.IsTrue(actual);
If the tests still pass, your code seems to do the job =)

Thanks in advance,

Sebi

haifisch

unread,
May 2, 2013, 8:54:43 AM5/2/13
to rhino...@googlegroups.com
Seems to work =) at least on my machine.

Sebastian Edelmeier

unread,
May 8, 2013, 6:59:07 AM5/8/13
to rhino...@googlegroups.com
Thanks then, I'll give your branch a try!

Sebastian Edelmeier

unread,
May 10, 2013, 3:33:05 AM5/10/13
to rhino...@googlegroups.com
Just to keep this updated: haifisch's branch really does the job.

Thanks for your kind support!
Reply all
Reply to author
Forward
0 new messages