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)) {
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.