Skip to content

Commit 80ffef9

Browse files
authored
Add some tests to cover #186 conditions (#189)
Fix issue with a new extension method
1 parent 111c52b commit 80ffef9

File tree

5 files changed

+101
-48
lines changed

5 files changed

+101
-48
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Ardalis.Result.Sample.Core.Services;
2+
3+
public class BadResultService
4+
{
5+
public Result ReturnErrorWithMessage(string message)
6+
{
7+
var result = Result.Error(message);
8+
return result;
9+
}
10+
11+
}

sample/Ardalis.Result.Sample.Core/Services/WeatherServiceWithExceptions.cs

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,69 +7,67 @@
77
using System.Linq;
88
using System.Threading.Tasks;
99

10-
namespace Ardalis.Result.Sample.Core.Services
10+
namespace Ardalis.Result.Sample.Core.Services;
11+
public class WeatherServiceWithExceptions
1112
{
12-
public class WeatherServiceWithExceptions
13+
public WeatherServiceWithExceptions(IStringLocalizer<WeatherService> stringLocalizer)
1314
{
14-
public WeatherServiceWithExceptions(IStringLocalizer<WeatherService> stringLocalizer)
15-
{
16-
_stringLocalizer = stringLocalizer;
17-
}
15+
_stringLocalizer = stringLocalizer;
16+
}
1817

19-
private static readonly string[] Summaries = new[]
18+
private static readonly string[] Summaries = new[]
2019
{
21-
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
22-
};
20+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
21+
};
2322

24-
private IStringLocalizer<WeatherService> _stringLocalizer;
23+
private IStringLocalizer<WeatherService> _stringLocalizer;
2524

26-
public Task<IEnumerable<WeatherForecast>> GetForecastAsync(ForecastRequestDto model)
25+
public Task<IEnumerable<WeatherForecast>> GetForecastAsync(ForecastRequestDto model)
26+
{
27+
return Task.FromResult(GetForecast(model));
28+
}
29+
30+
public IEnumerable<WeatherForecast> GetForecast(ForecastRequestDto model)
31+
{
32+
switch (model.PostalCode)
2733
{
28-
return Task.FromResult(GetForecast(model));
34+
case "NotFound":
35+
throw new ForecastNotFoundException();
36+
case "Conflict":
37+
throw new ForecastConflictException();
2938
}
3039

31-
public IEnumerable<WeatherForecast> GetForecast(ForecastRequestDto model)
40+
// validate model
41+
if (model.PostalCode.Length > 10)
3242
{
33-
switch (model.PostalCode)
43+
throw new ForecastRequestInvalidException(new Dictionary<string, string>()
3444
{
35-
case "NotFound":
36-
throw new ForecastNotFoundException();
37-
case "Conflict":
38-
throw new ForecastConflictException();
39-
}
45+
{ nameof(model.PostalCode), _stringLocalizer["PostalCode cannot exceed 10 characters."].Value }
46+
});
47+
}
4048

41-
// validate model
42-
if (model.PostalCode.Length > 10)
49+
// test single result value
50+
if (model.PostalCode == "55555")
51+
{
52+
return new List<WeatherForecast>
4353
{
44-
throw new ForecastRequestInvalidException(new Dictionary<string, string>()
54+
new WeatherForecast
4555
{
46-
{ nameof(model.PostalCode), _stringLocalizer["PostalCode cannot exceed 10 characters."].Value }
47-
});
48-
}
56+
Date = DateTime.Now,
57+
TemperatureC = 0,
58+
Summary = Summaries[0]
59+
}
60+
};
61+
}
4962

50-
// test single result value
51-
if (model.PostalCode == "55555")
63+
var rng = new Random();
64+
return new List<WeatherForecast>(Enumerable.Range(1, 5)
65+
.Select(index => new WeatherForecast
5266
{
53-
return new List<WeatherForecast>
54-
{
55-
new WeatherForecast
56-
{
57-
Date = DateTime.Now,
58-
TemperatureC = 0,
59-
Summary = Summaries[0]
60-
}
61-
};
62-
}
63-
64-
var rng = new Random();
65-
return new List<WeatherForecast>(Enumerable.Range(1, 5)
66-
.Select(index => new WeatherForecast
67-
{
68-
Date = DateTime.Now.AddDays(index),
69-
TemperatureC = rng.Next(-20, 55),
70-
Summary = Summaries[rng.Next(Summaries.Length)]
71-
})
72-
.ToArray());
73-
}
67+
Date = DateTime.Now.AddDays(index),
68+
TemperatureC = rng.Next(-20, 55),
69+
Summary = Summaries[rng.Next(Summaries.Length)]
70+
})
71+
.ToArray());
7472
}
7573
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Linq;
2+
using Ardalis.Result.Sample.Core.Services;
3+
using FluentAssertions;
4+
using Xunit;
5+
6+
namespace Ardalis.Result.Sample.UnitTests.ServiceTests;
7+
8+
public class BadResultService_ReturnResultWithError
9+
{
10+
[Fact]
11+
public void ReturnsErrorResultGivenMessage()
12+
{
13+
var service = new BadResultService();
14+
15+
var result = service.ReturnErrorWithMessage("Some error message");
16+
17+
result.Status.Should().Be(ResultStatus.Error);
18+
result.Errors.Single().Should().Be("Some error message");
19+
}
20+
}

src/Ardalis.Result/Result.Void.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ public static Result<T> Success<T>(T value, string successMessage)
6464
};
6565
}
6666

67+
/// <summary>
68+
/// Represents an error that occurred during the execution of the service.
69+
/// A single error message may be provided and will be exposed via the Errors property.
70+
/// </summary>
71+
/// <param name="errorMessage"></param>
72+
/// <returns></returns>
73+
public static Result Error(string errorMessage)
74+
{
75+
return new Result(ResultStatus.Error) { Errors = new[] { errorMessage } };
76+
}
77+
78+
6779
/// <summary>
6880
/// Represents the validation error that prevents the underlying service from completing.
6981
/// </summary>

tests/Ardalis.Result.UnitTests/ResultImplicitOperators.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Linq;
12
using Xunit;
23

34
namespace Ardalis.Result.UnitTests;
@@ -83,6 +84,17 @@ public void ConvertToNullObjectValue()
8384
Assert.Equal(expectedNullObject, result);
8485
}
8586

87+
[Fact]
88+
public void ConvertResultResultToResult()
89+
{
90+
var result = Result.Error(expectedString);
91+
92+
Result convertedResult = result;
93+
94+
Assert.NotNull(convertedResult);
95+
Assert.Equal(expectedString, convertedResult.Errors.First());
96+
}
97+
8698
public Result<T> DoBusinessOperationExample<T>(T testValue) => testValue;
8799
public T GetValueForResultExample<T>(Result<T> testResult) => testResult;
88100

0 commit comments

Comments
 (0)