Skip to content

Commit

Permalink
Tweak customerValidationTests
Browse files Browse the repository at this point in the history
Alter tests to use narrower ShouldHaveValidationErrorFor rather than
ShouldHaveAnyValidationError. Refactor to remove method call so that
each test is self explanatory
  • Loading branch information
donaldgray committed Jul 25, 2023
1 parent 8c7bcff commit dfa0aee
Showing 1 changed file with 41 additions and 25 deletions.
66 changes: 41 additions & 25 deletions src/protagonist/API.Tests/Customers/CustomerValidationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,15 @@ public CustomerValidationTests()
{
sut = new HydraCustomerValidator();
}

private Customer GetValidNewCustomer()

[Fact]
public void NewCustomer_RequiresOnly_Names()
{
return new Customer
var customer = new Customer
{
Name = "my-test-customer",
DisplayName = "My test customer"
};
}

[Fact]
public void NewCustomer_RequiresOnly_Names()
{
var customer = GetValidNewCustomer();

var result = sut.TestValidate(customer);

Expand All @@ -39,39 +34,60 @@ public void NewCustomer_Requires_Fields()

var result = sut.TestValidate(customer);

result.ShouldHaveAnyValidationError();
result.ShouldHaveValidationErrorFor(c => c.Name);
result.ShouldHaveValidationErrorFor(c => c.DisplayName);
}

[Fact]
public void NewCustomer_CannotBe_Admin()
{
var customer = GetValidNewCustomer();
customer.Administrator = true;

var customer = new Customer
{
Name = "my-test-customer",
DisplayName = "My test customer",
Administrator = true,
};

var result = sut.TestValidate(customer);

result.ShouldHaveAnyValidationError();
result.ShouldHaveValidationErrorFor(c => c.Administrator);
}

[Fact]
public void NewCustomer_CannotHaveName_Admin()
[Theory]
[InlineData("Admin")]
[InlineData("admin")]
[InlineData("ADMIN")]
public void NewCustomer_CannotHaveName_Admin(string invalidName)
{
var customer = GetValidNewCustomer();
customer.Name = "Admin";

var customer = new Customer
{
Name = invalidName,
DisplayName = "My test customer"
};

var result = sut.TestValidate(customer);

result.ShouldHaveAnyValidationError();
result
.ShouldHaveValidationErrorFor(c => c.Name)
.WithErrorMessage($"Name field [{invalidName}] cannot be a reserved word.");
}

[Fact]
public void NewCustomer_CannotHaveName_Version()
[Theory]
[InlineData("v2-customer")]
[InlineData("v2")]
[InlineData("v3")]
public void NewCustomer_CannotHaveName_Version(string invalidName)
{
var customer = GetValidNewCustomer();
customer.Name = "v2-customer";
var customer = new Customer
{
Name = invalidName,
DisplayName = "My test customer"
};

var result = sut.TestValidate(customer);

result.ShouldHaveAnyValidationError();
result
.ShouldHaveValidationErrorFor(c => c.Name)
.WithErrorMessage($"Name field [{invalidName}] cannot start with a version slug.");
}
}

0 comments on commit dfa0aee

Please sign in to comment.