Skip to content

Commit

Permalink
Fixed typos and formatting in "testing" docs (#2068)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristiqn-it committed Feb 16, 2023
1 parent d7d3599 commit 5cd8436
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ You can use the `TestValidate` extension method to invoke a validator for testin
For example, imagine the following validator is defined:

```csharp
public class PersonValidator : AbstractValidator<Person>
public class PersonValidator : AbstractValidator<Person>
{
public PersonValidator()
public PersonValidator()
{
RuleFor(person => person.Name).NotNull();
}
Expand All @@ -28,7 +28,7 @@ using FluentValidation;
using FluentValidation.TestHelper;

[TestFixture]
public class PersonValidatorTester
public class PersonValidatorTester
{
private PersonValidator validator;

Expand All @@ -39,15 +39,15 @@ public class PersonValidatorTester
}

[Test]
public void Should_have_error_when_Name_is_null()
public void Should_have_error_when_Name_is_null()
{
var model = new Person { Name = null };
var result = validator.TestValidate(model);
result.ShouldHaveValidationErrorFor(person => person.Name);
}

[Test]
public void Should_not_have_error_when_name_is_specified()
public void Should_not_have_error_when_name_is_specified()
{
var model = new Person { Name = "Jeremy" };
var result = validator.TestValidate(model);
Expand Down Expand Up @@ -85,7 +85,7 @@ result.ShouldHaveValidationErrorFor(person => person.Name)
.WithErrorCode("NotNullValidator");
```

If you want to make sure no other validation failures occured, except specified by conditions, use method `Only` after the conditions:
If you want to make sure no other validation failures occurred, except specified by conditions, use method `Only` after the conditions:

```csharp
var result = validator.TestValidate(person);
Expand All @@ -107,28 +107,28 @@ There is also an asynchronous `TestValidateAsync` method available which corresp

# Mocking

Validators are intended to be "black boxes" and we don't generally recommend mocking them. Within a test, the recommended appraoch is to supply a real validator instance with known bad data in order to trigger a validation error.
Validators are intended to be "black boxes" and we don't generally recommend mocking them. Within a test, the recommended approach is to supply a real validator instance with known bad data in order to trigger a validation error.

Mocking validators tends to require that you make assuptions about how the validators are built internally (both the rules contained within them, as well as FluentValidation's own internals). Mocking this behaviour leads to brittle tests that aren't upgrade-safe.
Mocking validators tends to require that you make assumptions about how the validators are built internally (both the rules contained within them, as well as FluentValidation's own internals). Mocking this behavior leads to brittle tests that aren't upgrade-safe.

However if you find yourself in a situation where you absoloutely do need to mock a validator, then we suggest using `InlineValidator<T>` to create a stub implementation as this way you can take advantage of re-using FluentValidation's own internal logic for creating validation failures. We *strongly* recommend not using a mocking library. An example of using `InlineValidator` is shown below:
However if you find yourself in a situation where you absolutely do need to mock a validator, then we suggest using `InlineValidator<T>` to create a stub implementation as this way you can take advantage of re-using FluentValidation's own internal logic for creating validation failures. We _strongly_ recommend not using a mocking library. An example of using `InlineValidator` is shown below:

```csharp
// Original validator that relies on an external service.
// External service is used to check that the customer ID is not already used in the database.
public class CustomerValidator : AbstractValidator<Customer>
public class CustomerValidator : AbstractValidator<Customer>
{
public CustomerValidator(ICustomerRepository customerRepository)
public CustomerValidator(ICustomerRepository customerRepository)
{
RuleFor(x => x.Id)
.Must(id => customerRepository.CheckIdNotInUse(id));
}
}

// If you needed to stub this failure in a unit/integration test,
// If you needed to stub this failure in a unit/integration test,
// you could do the following:
var validator = new InlineValidator<Customer>();
validator.RuleFor(x => x.Id).Must(id => false);
validator.RuleFor(x => x.Id).Must(id => false);

// This instance could then be passed into anywhere expecting an IValidator<Customer>
```

0 comments on commit 5cd8436

Please sign in to comment.