lundi 30 mars 2015

Does ShouldHaveValidationErrorFor exercise my code adequately?

If I have a Fluent Validator like this:



public class ContactDetailsViewModelValidator : AbstractValidator<ContactDetailsViewModel>
{
public ContactDetailsViewModelValidator()
{
RuleFor(x => x.PhoneNumberLandline)
.NotEmpty().When(x => string.IsNullOrEmpty(x.PhoneNumberMobile))
.WithValidationResource("Error_Resource_Empty_Landline")
.Matches(RegularExpressionConstants.LandlinePhoneNumber)
.WithValidationResource("Error_Resource_Format_Landline");
}
}


WithValidaitonResource is an extension method and is unfortunately implemented in such a way that if I was to do something like:



var validator = new ContactDetailsViewModelValidator();
valiidator.Validate()


It would fail due to internal dependencies on the class reading the resource file in WithValidaitonResource. I am on a large shared platform and refactoring to mock the dependencies would be difficult at present. I have noticed colleagues are currently unit testing similar validators using:



this.validator.ShouldHaveValidationErrorFor(x => x.PhoneNumberLandline, this.viewModel);


However, In the case of my method, I feel that this does not exercise the code adequately. It does test that the property has an error but not the correct one.


Is this all that can be done without fixing WithValidaitonResource so that I can call .validate() or is there a way to find if the correct error is present?


Thanks


Aucun commentaire:

Enregistrer un commentaire