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);
}// I want to test this section of codereturn true;Assert.IsFalse(actual);Assert.IsTrue(actual);