Skip to content

Commit 54b5de8

Browse files
authored
Fix 179 (#180)
* Working on fix for 179 * merge * Fix build warnings * Fixing tests Added fix for 179 Added test to confirm fix
1 parent ddbac3f commit 54b5de8

21 files changed

Lines changed: 149 additions & 39 deletions

File tree

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
4-
<AssemblyName>Ardalis.Result.Sample.Core</AssemblyName>
5-
<RootNamespace>Ardalis.Result.Sample.Core</RootNamespace>
6-
</PropertyGroup>
3+
<PropertyGroup>
4+
<AssemblyName>Ardalis.Result.Sample.Core</AssemblyName>
5+
<RootNamespace>Ardalis.Result.Sample.Core</RootNamespace>
6+
</PropertyGroup>
77

8-
<ItemGroup>
9-
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" />
10-
<PackageReference Include="System.ComponentModel.Annotations" />
11-
<PackageReference Include="System.Runtime" />
12-
</ItemGroup>
13-
14-
<ItemGroup>
15-
<ProjectReference Include="..\..\src\Ardalis.Result.FluentValidation\Ardalis.Result.FluentValidation.csproj" />
16-
<ProjectReference Include="..\..\src\Ardalis.Result\Ardalis.Result.csproj" />
17-
</ItemGroup>
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" />
10+
<PackageReference Include="System.ComponentModel.Annotations" />
11+
<PackageReference Include="System.Runtime" />
12+
</ItemGroup>
1813

14+
<ItemGroup>
15+
<ProjectReference Include="..\..\src\Ardalis.Result.FluentValidation\Ardalis.Result.FluentValidation.csproj" />
16+
<ProjectReference Include="..\..\src\Ardalis.Result\Ardalis.Result.csproj" />
17+
</ItemGroup>
1918
</Project>
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
namespace Ardalis.Result.Sample.Core.DTOs;
1+
using System;
2+
3+
namespace Ardalis.Result.Sample.Core.DTOs;
24

35
public class CreatePersonRequestDto
46
{
5-
public string FirstName { get; set; }
6-
public string LastName { get; set; }
7+
public string FirstName { get; set; } = String.Empty;
8+
public string LastName { get; set; } = String.Empty;
79
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11

2+
using System;
23
using System.ComponentModel.DataAnnotations;
34

45
namespace Ardalis.Result.Sample.Core.DTOs
56
{
67
public class ForecastRequestDto
78
{
89
[Required]
9-
public string PostalCode { get; set; }
10+
public string PostalCode { get; set; } = String.Empty;
1011
}
1112
}

sample/Ardalis.Result.Sample.Core/Exceptions/ForecastRequestInvalidException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Ardalis.Result.Sample.Core.Exceptions
55
{
66
public class ForecastRequestInvalidException : Exception
77
{
8-
public Dictionary<string,string> ValidationErrors { get; set; }
8+
public Dictionary<string,string> ValidationErrors { get; set; } = new();
99

1010
public ForecastRequestInvalidException(Dictionary<string, string> validationErrors) : base("Forecast request is invalid.")
1111
{

sample/Ardalis.Result.Sample.Core/Model/Person.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ namespace Ardalis.Result.Sample.Core.Model
66
public class Person
77
{
88
public int Id { get; set; }
9-
public string Surname { get; set; }
10-
public string Forename { get; set; }
9+
public string Surname { get; set; } = String.Empty;
10+
public string Forename { get; set; } = String.Empty;
1111

12-
public List<Person> Children { get; set; }
12+
public List<Person> Children { get; set; } = new();
1313

1414
public DateTime DateOfBirth { get; set; }
1515
}

sample/Ardalis.Result.Sample.Core/Model/WeatherForecast.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ public class WeatherForecast
1010

1111
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
1212

13-
public string Summary { get; set; }
13+
public string Summary { get; set; } = String.Empty;
1414
}
1515
}

sample/Ardalis.Result.SampleMinimalApi.FunctionalTests/NewWeatherForecast.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public async Task ReturnsOkWithValueGivenValidPostalCode()
3333
var stringResponse = await response.Content.ReadAsStringAsync();
3434
var forecasts = JsonConvert.DeserializeObject<List<WeatherForecast>>(stringResponse);
3535

36-
Assert.Equal("Freezing", forecasts.First().Summary);
36+
Assert.Equal("Freezing", forecasts?.First()?.Summary);
3737
}
3838

3939
[Fact]

sample/Ardalis.Result.SampleWeb.FunctionalTests/WeatherForecastControllerPost.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,30 @@
1313

1414
namespace Ardalis.Result.SampleWeb.FunctionalTests;
1515

16+
public class WeatherForecastControllerThrows : IClassFixture<WebApplicationFactory<WebMarker>>
17+
{
18+
private const string CONTROLLER_THROWS_ROUTE = "/weatherforecast/throws";
19+
private readonly HttpClient _client;
20+
21+
public WeatherForecastControllerThrows(WebApplicationFactory<WebMarker> factory)
22+
{
23+
_client = factory.CreateClient();
24+
}
25+
26+
[Fact]
27+
public async Task Returns400BadRequestNot500()
28+
{
29+
var response = await _client.GetAsync(CONTROLLER_THROWS_ROUTE);
30+
31+
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
32+
var stringResponse = await response.Content.ReadAsStringAsync();
33+
var problemDetails = JsonConvert.DeserializeObject<ProblemDetails>(stringResponse);
34+
35+
Assert.Equal("One or more validation errors occurred.", problemDetails?.Title);
36+
Assert.Equal(400, problemDetails.Status);
37+
}
38+
}
39+
1640
public class WeatherForecastControllerPost : IClassFixture<WebApplicationFactory<WebMarker>>
1741
{
1842
private const string CONTROLLER_POST_ROUTE = "/weatherforecast/create";
@@ -36,7 +60,7 @@ public async Task ReturnsOkWithValueGivenValidPostalCode(string route)
3660
var stringResponse = await response.Content.ReadAsStringAsync();
3761
var forecasts = JsonConvert.DeserializeObject<List<WeatherForecast>>(stringResponse);
3862

39-
Assert.Equal("Freezing", forecasts.First().Summary);
63+
Assert.Equal("Freezing", forecasts?.First()?.Summary);
4064
}
4165

4266
[Theory]

sample/Ardalis.Result.SampleWeb/MediatrApi/WeatherForecastController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public Task<Result<IEnumerable<WeatherForecast>>> CreateForecast([FromBody] NewF
4646
public class NewForecastCommand : IRequest<Result<IEnumerable<WeatherForecast>>>
4747
{
4848
[Required]
49-
public string PostalCode { get; set; }
49+
public string PostalCode { get; set; } = String.Empty;
5050
}
5151

5252
public class NewForecastHandler : IRequestHandler<NewForecastCommand, Result<IEnumerable<WeatherForecast>>>

sample/Ardalis.Result.SampleWeb/Pages/Index.cshtml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public IndexModel(IStringLocalizer<IndexModel> stringLocalizer)
1212
_stringLocalizer = stringLocalizer;
1313
}
1414

15-
public string Message { get; set; }
15+
public string Message { get; set; } = String.Empty;
1616
public void OnGet()
1717
{
1818
Message = _stringLocalizer["message"].Value;

0 commit comments

Comments
 (0)