Skip to content

Commit

Permalink
Add some tests to cover #186 conditions (#189)
Browse files Browse the repository at this point in the history
Fix issue with a new extension method
  • Loading branch information
ardalis committed May 25, 2024
1 parent 111c52b commit 80ffef9
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 48 deletions.
11 changes: 11 additions & 0 deletions sample/Ardalis.Result.Sample.Core/Services/BadResultService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Ardalis.Result.Sample.Core.Services;

public class BadResultService
{
public Result ReturnErrorWithMessage(string message)
{
var result = Result.Error(message);
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,69 +7,67 @@
using System.Linq;
using System.Threading.Tasks;

namespace Ardalis.Result.Sample.Core.Services
namespace Ardalis.Result.Sample.Core.Services;
public class WeatherServiceWithExceptions
{
public class WeatherServiceWithExceptions
public WeatherServiceWithExceptions(IStringLocalizer<WeatherService> stringLocalizer)
{
public WeatherServiceWithExceptions(IStringLocalizer<WeatherService> stringLocalizer)
{
_stringLocalizer = stringLocalizer;
}
_stringLocalizer = stringLocalizer;
}

private static readonly string[] Summaries = new[]
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

private IStringLocalizer<WeatherService> _stringLocalizer;
private IStringLocalizer<WeatherService> _stringLocalizer;

public Task<IEnumerable<WeatherForecast>> GetForecastAsync(ForecastRequestDto model)
public Task<IEnumerable<WeatherForecast>> GetForecastAsync(ForecastRequestDto model)
{
return Task.FromResult(GetForecast(model));
}

public IEnumerable<WeatherForecast> GetForecast(ForecastRequestDto model)
{
switch (model.PostalCode)
{
return Task.FromResult(GetForecast(model));
case "NotFound":
throw new ForecastNotFoundException();
case "Conflict":
throw new ForecastConflictException();
}

public IEnumerable<WeatherForecast> GetForecast(ForecastRequestDto model)
// validate model
if (model.PostalCode.Length > 10)
{
switch (model.PostalCode)
throw new ForecastRequestInvalidException(new Dictionary<string, string>()
{
case "NotFound":
throw new ForecastNotFoundException();
case "Conflict":
throw new ForecastConflictException();
}
{ nameof(model.PostalCode), _stringLocalizer["PostalCode cannot exceed 10 characters."].Value }
});
}

// validate model
if (model.PostalCode.Length > 10)
// test single result value
if (model.PostalCode == "55555")
{
return new List<WeatherForecast>
{
throw new ForecastRequestInvalidException(new Dictionary<string, string>()
new WeatherForecast
{
{ nameof(model.PostalCode), _stringLocalizer["PostalCode cannot exceed 10 characters."].Value }
});
}
Date = DateTime.Now,
TemperatureC = 0,
Summary = Summaries[0]
}
};
}

// test single result value
if (model.PostalCode == "55555")
var rng = new Random();
return new List<WeatherForecast>(Enumerable.Range(1, 5)
.Select(index => new WeatherForecast
{
return new List<WeatherForecast>
{
new WeatherForecast
{
Date = DateTime.Now,
TemperatureC = 0,
Summary = Summaries[0]
}
};
}

var rng = new Random();
return new List<WeatherForecast>(Enumerable.Range(1, 5)
.Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray());
}
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Linq;
using Ardalis.Result.Sample.Core.Services;
using FluentAssertions;
using Xunit;

namespace Ardalis.Result.Sample.UnitTests.ServiceTests;

public class BadResultService_ReturnResultWithError
{
[Fact]
public void ReturnsErrorResultGivenMessage()
{
var service = new BadResultService();

var result = service.ReturnErrorWithMessage("Some error message");

result.Status.Should().Be(ResultStatus.Error);
result.Errors.Single().Should().Be("Some error message");
}
}
12 changes: 12 additions & 0 deletions src/Ardalis.Result/Result.Void.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ public new static Result Error(ErrorList error = null)
};
}

/// <summary>
/// Represents an error that occurred during the execution of the service.
/// A single error message may be provided and will be exposed via the Errors property.
/// </summary>
/// <param name="errorMessage"></param>
/// <returns></returns>
public static Result Error(string errorMessage)

Check warning on line 73 in src/Ardalis.Result/Result.Void.cs

View workflow job for this annotation

GitHub Actions / build

'Result.Error(string)' hides inherited member 'Result<Result>.Error(string)'. Use the new keyword if hiding was intended.

Check warning on line 73 in src/Ardalis.Result/Result.Void.cs

View workflow job for this annotation

GitHub Actions / build

'Result.Error(string)' hides inherited member 'Result<Result>.Error(string)'. Use the new keyword if hiding was intended.

Check warning on line 73 in src/Ardalis.Result/Result.Void.cs

View workflow job for this annotation

GitHub Actions / build

'Result.Error(string)' hides inherited member 'Result<Result>.Error(string)'. Use the new keyword if hiding was intended.

Check warning on line 73 in src/Ardalis.Result/Result.Void.cs

View workflow job for this annotation

GitHub Actions / build

'Result.Error(string)' hides inherited member 'Result<Result>.Error(string)'. Use the new keyword if hiding was intended.

Check warning on line 73 in src/Ardalis.Result/Result.Void.cs

View workflow job for this annotation

GitHub Actions / build

'Result.Error(string)' hides inherited member 'Result<Result>.Error(string)'. Use the new keyword if hiding was intended.

Check warning on line 73 in src/Ardalis.Result/Result.Void.cs

View workflow job for this annotation

GitHub Actions / build

'Result.Error(string)' hides inherited member 'Result<Result>.Error(string)'. Use the new keyword if hiding was intended.

Check warning on line 73 in src/Ardalis.Result/Result.Void.cs

View workflow job for this annotation

GitHub Actions / build

'Result.Error(string)' hides inherited member 'Result<Result>.Error(string)'. Use the new keyword if hiding was intended.

Check warning on line 73 in src/Ardalis.Result/Result.Void.cs

View workflow job for this annotation

GitHub Actions / build

'Result.Error(string)' hides inherited member 'Result<Result>.Error(string)'. Use the new keyword if hiding was intended.

Check warning on line 73 in src/Ardalis.Result/Result.Void.cs

View workflow job for this annotation

GitHub Actions / build

'Result.Error(string)' hides inherited member 'Result<Result>.Error(string)'. Use the new keyword if hiding was intended.

Check warning on line 73 in src/Ardalis.Result/Result.Void.cs

View workflow job for this annotation

GitHub Actions / build

'Result.Error(string)' hides inherited member 'Result<Result>.Error(string)'. Use the new keyword if hiding was intended.

Check warning on line 73 in src/Ardalis.Result/Result.Void.cs

View workflow job for this annotation

GitHub Actions / build

'Result.Error(string)' hides inherited member 'Result<Result>.Error(string)'. Use the new keyword if hiding was intended.

Check warning on line 73 in src/Ardalis.Result/Result.Void.cs

View workflow job for this annotation

GitHub Actions / build

'Result.Error(string)' hides inherited member 'Result<Result>.Error(string)'. Use the new keyword if hiding was intended.
{
return new Result(ResultStatus.Error) { Errors = new[] { errorMessage } };
}


/// <summary>
/// Represents the validation error that prevents the underlying service from completing.
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions tests/Ardalis.Result.UnitTests/ResultImplicitOperators.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Linq;
using Xunit;

namespace Ardalis.Result.UnitTests;
Expand Down Expand Up @@ -83,6 +84,17 @@ public void ConvertToNullObjectValue()
Assert.Equal(expectedNullObject, result);
}

[Fact]
public void ConvertResultResultToResult()
{
var result = Result.Error(expectedString);

Result convertedResult = result;

Assert.NotNull(convertedResult);
Assert.Equal(expectedString, convertedResult.Errors.First());
}

public Result<T> DoBusinessOperationExample<T>(T testValue) => testValue;
public T GetValueForResultExample<T>(Result<T> testResult) => testResult;

Expand Down

0 comments on commit 80ffef9

Please sign in to comment.