Skip to content

Commit a02ee36

Browse files
Updated code with better syntax and .NET 6 project.
1 parent 1fd5b50 commit a02ee36

18 files changed

Lines changed: 107 additions & 200 deletions

netcore-unit-testing/web-api/Model/ShoppingItem.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

netcore-unit-testing/web-api/Program.cs

Lines changed: 0 additions & 26 deletions
This file was deleted.

netcore-unit-testing/web-api/Services/ShoppingCartServices.cs

Lines changed: 0 additions & 30 deletions
This file was deleted.

netcore-unit-testing/web-api/Startup.cs

Lines changed: 0 additions & 45 deletions
This file was deleted.

netcore-unit-testing/web-api/appsettings.Development.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

netcore-unit-testing/web-api-tests/ShoppingCartControllerTest.cs renamed to netcore-unit-testing/web-api/web-api-tests/ShoppingCartControllerTest.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Linq;
5-
using System.Text;
65
using web_api.Contracts;
76
using web_api.Controllers;
87
using web_api.Model;
@@ -12,8 +11,8 @@ namespace web_api_tests
1211
{
1312
public class ShoppingCartControllerTest
1413
{
15-
ShoppingCartController _controller;
16-
IShoppingCartService _service;
14+
private readonly ShoppingCartController _controller;
15+
private readonly IShoppingCartService _service;
1716

1817
public ShoppingCartControllerTest()
1918
{
@@ -28,14 +27,14 @@ public void Get_WhenCalled_ReturnsOkResult()
2827
var okResult = _controller.Get();
2928

3029
// Assert
31-
Assert.IsType<OkObjectResult>(okResult.Result);
30+
Assert.IsType<OkObjectResult>(okResult as OkObjectResult);
3231
}
3332

3433
[Fact]
3534
public void Get_WhenCalled_ReturnsAllItems()
3635
{
3736
// Act
38-
var okResult = _controller.Get().Result as OkObjectResult;
37+
var okResult = _controller.Get() as OkObjectResult;
3938

4039
// Assert
4140
var items = Assert.IsType<List<ShoppingItem>>(okResult.Value);
@@ -49,7 +48,7 @@ public void GetById_UnknownGuidPassed_ReturnsNotFoundResult()
4948
var notFoundResult = _controller.Get(Guid.NewGuid());
5049

5150
// Assert
52-
Assert.IsType<NotFoundResult>(notFoundResult.Result);
51+
Assert.IsType<NotFoundResult>(notFoundResult);
5352
}
5453

5554
[Fact]
@@ -62,7 +61,7 @@ public void GetById_ExistingGuidPassed_ReturnsOkResult()
6261
var okResult = _controller.Get(testGuid);
6362

6463
// Assert
65-
Assert.IsType<OkObjectResult>(okResult.Result);
64+
Assert.IsType<OkObjectResult>(okResult as OkObjectResult);
6665
}
6766

6867
[Fact]
@@ -72,7 +71,7 @@ public void GetById_ExistingGuidPassed_ReturnsRightItem()
7271
var testGuid = new Guid("ab2bd817-98cd-4cf3-a80a-53ea0cd9c200");
7372

7473
// Act
75-
var okResult = _controller.Get(testGuid).Result as OkObjectResult;
74+
var okResult = _controller.Get(testGuid) as OkObjectResult;
7675

7776
// Assert
7877
Assert.IsType<ShoppingItem>(okResult.Value);
@@ -97,7 +96,6 @@ public void Add_InvalidObjectPassed_ReturnsBadRequest()
9796
Assert.IsType<BadRequestObjectResult>(badResponse);
9897
}
9998

100-
10199
[Fact]
102100
public void Add_ValidObjectPassed_ReturnsCreatedResponse()
103101
{
@@ -116,7 +114,6 @@ public void Add_ValidObjectPassed_ReturnsCreatedResponse()
116114
Assert.IsType<CreatedAtActionResult>(createdResponse);
117115
}
118116

119-
120117
[Fact]
121118
public void Add_ValidObjectPassed_ReturnedResponseHasCreatedItem()
122119
{
@@ -151,16 +148,16 @@ public void Remove_NotExistingGuidPassed_ReturnsNotFoundResponse()
151148
}
152149

153150
[Fact]
154-
public void Remove_ExistingGuidPassed_ReturnsOkResult()
151+
public void Remove_ExistingGuidPassed_ReturnsNoContentResult()
155152
{
156153
// Arrange
157154
var existingGuid = new Guid("ab2bd817-98cd-4cf3-a80a-53ea0cd9c200");
158155

159156
// Act
160-
var okResponse = _controller.Remove(existingGuid);
157+
var noContentResponse = _controller.Remove(existingGuid);
161158

162159
// Assert
163-
Assert.IsType<OkResult>(okResponse);
160+
Assert.IsType<NoContentResult>(noContentResponse);
164161
}
165162

166163
[Fact]

netcore-unit-testing/web-api-tests/ShoppingCartServiceFake.cs renamed to netcore-unit-testing/web-api/web-api-tests/ShoppingCartServiceFake.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.Text;
54
using web_api.Contracts;
65
using web_api.Model;
76

87
namespace web_api_tests
98
{
10-
public class ShoppingCartServiceFake : IShoppingCartService
9+
public class ShoppingCartServiceFake : IShoppingCartService
1110
{
1211
private readonly List<ShoppingItem> _shoppingCart;
1312

netcore-unit-testing/web-api-tests/web-api-tests.csproj renamed to netcore-unit-testing/web-api/web-api-tests/web-api-tests.csproj

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net5.0</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55
<RootNamespace>web_api_tests</RootNamespace>
6+
<Nullable>enable</Nullable>
67

78
<IsPackable>false</IsPackable>
89
</PropertyGroup>
910

1011
<ItemGroup>
11-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
1213
<PackageReference Include="xunit" Version="2.4.1" />
1314
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
14-
<PrivateAssets>all</PrivateAssets>
1515
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
16-
</PackageReference>
17-
<PackageReference Include="coverlet.collector" Version="3.0.3">
1816
<PrivateAssets>all</PrivateAssets>
17+
</PackageReference>
18+
<PackageReference Include="coverlet.collector" Version="3.0.2">
1919
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
20+
<PrivateAssets>all</PrivateAssets>
2021
</PackageReference>
2122
</ItemGroup>
2223

netcore-unit-testing/web-api/web-api.csproj

Lines changed: 0 additions & 18 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.29519.87
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31717.71
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "web-api", "web-api\web-api.csproj", "{39CB9E19-0069-4404-B3BF-4FB79E3E38BD}"
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "web-api", "web-api\web-api.csproj", "{47DF286E-1D20-479F-88B3-04F7B6FEF69A}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "web-api-tests", "web-api-tests\web-api-tests.csproj", "{1745BC82-3BD5-40B6-8B3C-EE430CF242DF}"
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "web-api-tests", "web-api-tests\web-api-tests.csproj", "{E53C6906-F4A3-4B46-9458-27457AADCDEF}"
99
EndProject
1010
Global
1111
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1212
Debug|Any CPU = Debug|Any CPU
1313
Release|Any CPU = Release|Any CPU
1414
EndGlobalSection
1515
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16-
{39CB9E19-0069-4404-B3BF-4FB79E3E38BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17-
{39CB9E19-0069-4404-B3BF-4FB79E3E38BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
18-
{39CB9E19-0069-4404-B3BF-4FB79E3E38BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
19-
{39CB9E19-0069-4404-B3BF-4FB79E3E38BD}.Release|Any CPU.Build.0 = Release|Any CPU
20-
{1745BC82-3BD5-40B6-8B3C-EE430CF242DF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21-
{1745BC82-3BD5-40B6-8B3C-EE430CF242DF}.Debug|Any CPU.Build.0 = Debug|Any CPU
22-
{1745BC82-3BD5-40B6-8B3C-EE430CF242DF}.Release|Any CPU.ActiveCfg = Release|Any CPU
23-
{1745BC82-3BD5-40B6-8B3C-EE430CF242DF}.Release|Any CPU.Build.0 = Release|Any CPU
16+
{47DF286E-1D20-479F-88B3-04F7B6FEF69A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{47DF286E-1D20-479F-88B3-04F7B6FEF69A}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{47DF286E-1D20-479F-88B3-04F7B6FEF69A}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{47DF286E-1D20-479F-88B3-04F7B6FEF69A}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{E53C6906-F4A3-4B46-9458-27457AADCDEF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{E53C6906-F4A3-4B46-9458-27457AADCDEF}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{E53C6906-F4A3-4B46-9458-27457AADCDEF}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{E53C6906-F4A3-4B46-9458-27457AADCDEF}.Release|Any CPU.Build.0 = Release|Any CPU
2424
EndGlobalSection
2525
GlobalSection(SolutionProperties) = preSolution
2626
HideSolutionNode = FALSE
2727
EndGlobalSection
2828
GlobalSection(ExtensibilityGlobals) = postSolution
29-
SolutionGuid = {FA769AC7-98B1-4C15-96B1-E2B394BE8835}
29+
SolutionGuid = {1BCBE668-3013-4BAD-AAEB-11875001143C}
3030
EndGlobalSection
3131
EndGlobal

0 commit comments

Comments
 (0)