Correctable link parameter with RhinoMocks

I got my code, I have the following call:

string proposed=string.Empty; validator.IsValid(arg0, arg1, ref proposed); 

I am completing the validator in my test and want this stub to modify the contents of the proposed reference string variable. I tried to set the value of the argument in the WhenCalled handler, but this has no effect.

 validatorStub.Stub(x => x.IsValid(arg0, arg1, ref proposed)) .IgnoreArguments() .WhenCalled(invocation => { invocation.Arguments[2] = "123456"; }).Throw(new ValidationException(string.Empty)); 

Is this possible with Rhino? Unfortunately, I have no way to edit this validator ...

EDIT: Thanks to @FireAlkazar, I realized that I need to better illustrate my test situtation:

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); } 

However, when I move on to this, I see that the ValidatorStub throws an exception, which I expect it to throw, but never sets the ref parameter.


EDIT: This RhinoMocks thread uses a newer version of Castle Core that solves the problem. The author was kind enough to report this through Google groups.

+4
source share
1 answer

Documentation for this case Rhino Mocks 3.5 Output Arguments and Links

It looks like you will have something like

 validatorStub.Stub(x => x.IsValid(Arg<string>.Is.Anything, Arg<string>.Is.Anything, ref Arg<string>.Ref(Rhino.Mocks.Constraints.Is.Anything(), "123456").Dummy)); string testRefValue = ""; validatorStub.IsValid("1", "2", ref testRefValue); Assert.AreEqual("123456", testRefValue); 

EDIT:
Investigate your case. The end result is no, it cannot do this in the latest version of Rhino Mocks (3.6). The reason is an error in the old version of Castle.DynamycProxy, which is used by mocks.

Evidence
Bugfix: the ref and out parameter could not be obtained if the Proxied Method throw this patch adds lines to Castle.Core / DynamicProxy / Generators / InvocationTypeGenerator.cs as follows:

 bool hasByRefArguments = false; //... if (hasByRefArguments) { invokeMethodOnTarget.CodeBuilder.AddStatement(new TryStatement()); } //... 

Add to the reflector for Rhino.Mocks.dll there is no additional processing for case hasByRefArguments (see the same file InvocationTypeGenerator.cs).

+6
source

All Articles