I have a unit test that I'm trying to build, that should check for other Organisation names and throw out an error message if they already exist (it needs to be unique).
Could someone tell me why this keeps asserting as True when it should be False?
[Fact]
public async Task IsValid_ReturnsFalse_WhenNameAlreadyExists()
{
// Arrange
var input = new Organisation
{
Name = "TestClient",
Id = "TestOrg"
};
var mockOrganisationService = new Mock<IOrganisationService>();
mockOrganisationService
.Setup(x => x.GetOrganisationByNameAsync(It.IsAny<string>()))
.ReturnsAsync((Organisation)null);
var inputCopy = new Organisation
{
Name = "TestClient",
Id = "TestOrg"
};
var myValidation = new ClientValidation(mockOrganisationService.Object);
// Act
var result = await myValidation.IsValid(input);
var resultCopy = await myValidation.IsValid(inputCopy);
// Assert
Assert.True(result.Valid);
Assert.False(resultCopy.Valid);
}
CodePudding user response:
Turns out I've been going about it all wrong, the Moq can't return an Organisation to validate against another as it can only handle it one at a time. I was misinformed by my dev. Thanks for trying to help anyway!
[Fact]
public async Task IsValid_ReturnsTrue_WhenCreatingANewClient()
{
// Arrange
var input = new Organisation
{
Name = "TestClient",
Id = "TestOrg"
};
var mockOrganisationService = new Mock<IOrganisationService>();
mockOrganisationService
.Setup(x => x.GetOrganisationNameAsync(It.IsAny<String>())).Returns(() => new ValueTask<Organisation>(input));
var myValidation = new ClientValidation(mockOrganisationService.Object);
// Act
var result = await myValidation.IsValid(input);
// Assert
Assert.True(result.Valid);
}