Skip to content

Commit a3a4f7c

Browse files
committed
add unit test and integration test sample
1 parent bb536a7 commit a3a4f7c

File tree

4 files changed

+122
-1
lines changed

4 files changed

+122
-1
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+

2+
using System.ComponentModel;
3+
using System.Linq;
4+
using System.Net;
5+
using System.Threading.Tasks;
6+
using GenFu;
7+
using Microsoft.AspNetCore.Http;
8+
using Microsoft.AspNetCore.Mvc;
9+
using Moq;
10+
using SampleWebApiAspNetCore.Entities;
11+
using SampleWebApiAspNetCore.Models;
12+
using SampleWebApiAspNetCore.Repositories;
13+
using SampleWebApiAspNetCore.v1.Controllers;
14+
using Xunit;
15+
16+
namespace SampleWebApiAspNetCore.Test.ControllerTests.v1
17+
{
18+
public class FoodsControllerTests
19+
{
20+
private FoodsController _controller;
21+
private Mock<IFoodRepository> _repository;
22+
private Mock<IUrlHelper> _urlHelper;
23+
24+
public FoodsControllerTests()
25+
{
26+
_repository = new Mock<IFoodRepository>();
27+
_urlHelper = new Mock<IUrlHelper>();
28+
29+
_controller = new FoodsController(_urlHelper.Object, _repository.Object)
30+
{
31+
ControllerContext = new ControllerContext
32+
{
33+
HttpContext = new DefaultHttpContext()
34+
}
35+
};
36+
}
37+
38+
[Fact]
39+
[Category("Unit Test")]
40+
public void Get_All_Food_Success()
41+
{
42+
// Arrange
43+
var foods = A.ListOf<FoodItem>(10).AsQueryable();
44+
_repository.Setup(x => x.GetAll(It.IsAny<QueryParameters>())).Returns(foods);
45+
_repository.Setup(x => x.Count()).Returns(foods.Count);
46+
47+
// Act
48+
var result = _controller.GetAllFoods(new QueryParameters()) as OkObjectResult;
49+
50+
// Assert
51+
Assert.NotNull(result);
52+
Assert.Equal(200, result.StatusCode);
53+
}
54+
55+
[Fact]
56+
[Category("Integration Test")]
57+
public async Task Get_All_Food_Success_Integration_Test()
58+
{
59+
using (var httpClient = new TestClientProvider().Client)
60+
{
61+
// Arrange
62+
63+
// Act
64+
var result = await httpClient.GetAsync("api/v1/Foods?Page=1&PageCount=5");
65+
66+
// Assert
67+
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
68+
}
69+
}
70+
}
71+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.1</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="GenFu" Version="1.4.22" />
11+
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.1" />
12+
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.1.1" />
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
14+
<PackageReference Include="Moq" Version="4.10.0" />
15+
<PackageReference Include="xunit" Version="2.3.1" />
16+
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<ProjectReference Include="..\SampleWebApiAspNetCore\SampleWebApiAspNetCore.csproj" />
21+
</ItemGroup>
22+
23+
</Project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Net.Http;
2+
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.AspNetCore.TestHost;
4+
using WebApplication11;
5+
6+
namespace SampleWebApiAspNetCore.Test
7+
{
8+
public class TestClientProvider
9+
{
10+
public HttpClient Client { get; private set; }
11+
12+
public TestClientProvider()
13+
{
14+
var server = new TestServer(
15+
new WebHostBuilder()
16+
.UseStartup<Startup>());
17+
18+
Client = server.CreateClient();
19+
}
20+
}
21+
}

SampleWebApiAspNetCore.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
44
VisualStudioVersion = 15.0.27703.2026
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleWebApiAspNetCore", "SampleWebApiAspNetCore\SampleWebApiAspNetCore.csproj", "{62E0EE7C-8CF6-4E04-B3A1-49A19DE3FC75}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleWebApiAspNetCore", "SampleWebApiAspNetCore\SampleWebApiAspNetCore.csproj", "{62E0EE7C-8CF6-4E04-B3A1-49A19DE3FC75}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleWebApiAspNetCore.Test", "SampleWebApiAspNetCore.Test\SampleWebApiAspNetCore.Test.csproj", "{34867F50-D123-4211-82F1-9561725F7833}"
79
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -15,6 +17,10 @@ Global
1517
{62E0EE7C-8CF6-4E04-B3A1-49A19DE3FC75}.Debug|Any CPU.Build.0 = Debug|Any CPU
1618
{62E0EE7C-8CF6-4E04-B3A1-49A19DE3FC75}.Release|Any CPU.ActiveCfg = Release|Any CPU
1719
{62E0EE7C-8CF6-4E04-B3A1-49A19DE3FC75}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{34867F50-D123-4211-82F1-9561725F7833}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{34867F50-D123-4211-82F1-9561725F7833}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{34867F50-D123-4211-82F1-9561725F7833}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{34867F50-D123-4211-82F1-9561725F7833}.Release|Any CPU.Build.0 = Release|Any CPU
1824
EndGlobalSection
1925
GlobalSection(SolutionProperties) = preSolution
2026
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)