Skip to content

Commit d7cfce6

Browse files
committed
updates to tests
1 parent 522afa5 commit d7cfce6

File tree

8 files changed

+176
-39
lines changed

8 files changed

+176
-39
lines changed

.devcontainer/tests/devcontainer.json

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
2+
// README at: https://github.com/devcontainers/templates/tree/main/src/docker-existing-docker-compose
3+
{
4+
"name": ".NET API Tests",
5+
6+
// Update the 'dockerComposeFile' list if you have more compose files or use different names.
7+
// The .devcontainer/docker-compose.yml file contains any overrides you need/want to make.
8+
"dockerComposeFile": [
9+
"../../docker-compose.yml"
10+
],
11+
12+
// The 'service' property is the name of the service for the container that VS Code should
13+
// use. Update this value and .devcontainer/docker-compose.yml to the real service name.
14+
"service": "tests",
15+
16+
// The optional 'workspaceFolder' property is the path VS Code should open by default when
17+
// connected. This is typically a file mount in .devcontainer/docker-compose.yml
18+
"workspaceFolder": "/workspace/tests",
19+
20+
// Features to add to the dev container. More info: https://containers.dev/features.
21+
22+
23+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
24+
// "forwardPorts": [],
25+
26+
// Uncomment the next line if you want start specific services in your Docker Compose config.
27+
// "runServices": [],
28+
29+
// Uncomment the next line if you want to keep your containers running after VS Code shuts down.
30+
"shutdownAction": "none",
31+
"customizations": {
32+
"vscode": {
33+
"extensions": [
34+
"ms-dotnettools.csharp",
35+
"rangav.vscode-thunder-client",
36+
"GitHub.copilot",
37+
"ms-dotnettools.csdevkit"
38+
]
39+
}
40+
}
41+
42+
// Uncomment the next line to run commands after the container is created.
43+
//"postCreateCommand": "chmod +x ./setup.sh; ./setup.sh"
44+
45+
46+
// Configure tool-specific properties.
47+
// "customizations": {},
48+
49+
// Uncomment to connect as an existing user other than the container default. More info: https://aka.ms/dev-containers-non-root.
50+
// "remoteUser": "devcontainer"
51+
}
52+
53+
54+
55+

api/Counter.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@
22

33
namespace Api.Function
44
{
5-
public class Counter
5+
public class Counter(string id, int count)
66
{
77
[JsonPropertyName("id")]
8-
public string Id { get; set; }
8+
public string Id { get; set; } = id ?? throw new ArgumentNullException(nameof(id));
99

1010
[JsonPropertyName("count")]
11-
public int Count { get; set; }
12-
public Counter(string id)
13-
{
14-
Id = id ?? throw new ArgumentNullException(nameof(id));
15-
}
11+
public int Count { get; set; } = count;
1612
}
1713
}

docker-compose.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ services:
2020
command: sleep infinity
2121
networks:
2222
- mynet
23+
24+
tests:
25+
image: mcr.microsoft.com/devcontainers/dotnet:8.0-bookworm
26+
volumes:
27+
# Mount the root folder that contains .git
28+
- .:/workspace:cached
29+
command: sleep infinity
30+
links:
31+
- api
32+
networks:
33+
- mynet
34+
# ...
2335

2436
networks:
2537
mynet:

frontend/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<!--- Basic Page Needs
88
================================================== -->
99
<meta charset="utf-8">
10-
<title>Gwyn's Resume</title>
10+
<title>GPS's Azure Resume</title>
1111
<meta name="description" content="">
1212
<meta name="author" content="">
1313

tests/README.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/TestCounter.cs

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,73 @@
1-
using Microsoft.AspNetCore.Mvc;
2-
using Microsoft.Extensions.Logging;
3-
using Xunit;
4-
using Microsoft.Azure.WebJobs;
5-
using Microsoft.Azure.WebJobs.Extensions.Http;
1+
using Api.Function;
62
using Microsoft.AspNetCore.Http;
3+
using Microsoft.Azure.Functions.Worker;
4+
using Microsoft.Azure.Functions.Worker.Http;
75
using Microsoft.Extensions.Logging;
8-
using Newtonsoft.Json;
6+
using Moq;
7+
using System.IO;
98
using System.Net;
10-
using System.Net.Http;
119
using System.Text;
10+
using System.Threading.Tasks;
11+
using Xunit;
1212

13-
namespace tests
13+
public class GetVisitorCounterTests
1414
{
15-
public class TestCounter
15+
private readonly ILogger<GetVisitorCounter> logger;
16+
private readonly Mock<HttpRequestData> mockRequest;
17+
private readonly Mock<FunctionContext> mockFunctionContext;
18+
19+
public GetVisitorCounterTests()
20+
{
21+
logger = Mock.Of<ILogger<GetVisitorCounter>>();
22+
mockFunctionContext = new Mock<FunctionContext>();
23+
mockRequest = new Mock<HttpRequestData>(MockBehavior.Strict, mockFunctionContext.Object);
24+
25+
mockRequest.Setup(req => req.CreateResponse()).Returns(() => CreateMockHttpResponseData(mockFunctionContext.Object));
26+
}
27+
28+
29+
[Fact]
30+
public async Task Run_ShouldIncrementCounter()
1631
{
17-
private readonly ILogger logger = TestFactory.CreateLogger();
18-
19-
[Fact]
20-
public async void Http_trigger_should_return_known_string()
21-
{
22-
var counter = new Company.Function.Counter();
23-
counter.Id = "1";
24-
counter.Count = 2;
25-
var request = TestFactory.CreateHttpRequest();
26-
var response = (HttpResponseMessage) Company.Function.GetResumeCounter.Run(request, counter, out counter, logger);
27-
Assert.Equal(3, counter.Count);
28-
}
32+
// Arrange
33+
var counter = new Counter(id : "index"){ Count = 1 };
34+
var function = new GetVisitorCounter(logger);
35+
var loggerFactoryMock = new Mock<ILoggerFactory>();
36+
var loggerMock = new Mock<ILogger>();
37+
loggerFactoryMock.Setup(x => x.CreateLogger(It.IsAny<string>())).Returns(loggerMock.Object);
38+
var requestMock = new Mock<HttpRequestData>();
39+
var responseMock = new Mock<HttpResponseData>();
40+
41+
// Simulate the function call
42+
var response = function.Run(requestMock.Object, counter);
2943

44+
// Assert
45+
Assert.NotNull(response);
46+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
47+
48+
// Verify that the response contains the expected content type
49+
Assert.True(response.Headers.TryGetValues("Content-Type", out var contentTypes));
50+
Assert.Contains("text/plain; charset=utf-8", contentTypes);
51+
52+
// Act
53+
var result = await function.Run(mockRequest.Object, counter);
54+
55+
// Assert
56+
Assert.NotNull(result);
57+
Assert.Equal(2, result.NewCounter.Count); // Check if the counter is incremented
58+
// Additional assertions as necessary
3059
}
31-
}
60+
}
61+
62+
// Arrange
63+
64+
65+
66+
67+
68+
69+
70+
71+
72+
73+

tests/tests.csproj

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
32
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.1</TargetFramework>
5-
3+
<TargetFramework>net8.0</TargetFramework>
64
<IsPackable>false</IsPackable>
75
</PropertyGroup>
86

97
<ItemGroup>
10-
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
11-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
12-
<PackageReference Include="xunit" Version="2.4.1" />
13-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
8+
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
9+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
10+
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
12+
<PackageReference Include="Moq" Version="4.20.69" />
13+
<PackageReference Include="xunit" Version="2.4.2" />
14+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
1415
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1516
<PrivateAssets>all</PrivateAssets>
1617
</PackageReference>
17-
<PackageReference Include="coverlet.collector" Version="1.3.0">
18+
<PackageReference Include="coverlet.collector" Version="6.0.0">
1819
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1920
<PrivateAssets>all</PrivateAssets>
2021
</PackageReference>
@@ -24,4 +25,5 @@
2425
<ProjectReference Include="..\api\api.csproj" />
2526
</ItemGroup>
2627

28+
2729
</Project>

tests/tests.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.5.002.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "tests", "tests.csproj", "{C077F20F-7BF0-4112-84C9-AFE8DD6F2D66}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "testing1", "testing1\testing1.csproj", "{BAB3BC90-56E1-4FA6-BF08-01919496262C}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{C077F20F-7BF0-4112-84C9-AFE8DD6F2D66}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{C077F20F-7BF0-4112-84C9-AFE8DD6F2D66}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{C077F20F-7BF0-4112-84C9-AFE8DD6F2D66}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{C077F20F-7BF0-4112-84C9-AFE8DD6F2D66}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{BAB3BC90-56E1-4FA6-BF08-01919496262C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{BAB3BC90-56E1-4FA6-BF08-01919496262C}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{BAB3BC90-56E1-4FA6-BF08-01919496262C}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{BAB3BC90-56E1-4FA6-BF08-01919496262C}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {09FE1130-5B6A-4B10-B953-AB072EAE676D}
30+
EndGlobalSection
31+
EndGlobal

0 commit comments

Comments
 (0)