Skip to content

Commit

Permalink
add tests?
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidMatthew1987 committed Jun 20, 2024
1 parent 7dfe2ba commit 0de45f1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This workflow will build a .NET project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net

name: test

on:
[push, pull_request]

jobs:
test:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-restore

34 changes: 34 additions & 0 deletions Tests/AnalyticsRepositoryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Data;
using NSubstitute;
using Xunit;
using AnalyticsWebApi.Datamodel;
using Dapper;

namespace AnalyticsWebApi;

public class AnalyticsRepositoryTests
{
[Fact]
public async Task SaveAnalyticsData_ValidData_ReturnsSuccess()
{
// Arrange
var mockConnection = Substitute.For<IDbConnection>();
var repository = new AnalyticsRepository(""); // Pass an empty string here
var data = new AnalyticsData {
Referer = "https://www.example.com",
IpAddress = "127.0.0.1",
DataKey = "test-key",
Data = "test-data"
};

mockConnection.ExecuteAsync(Arg.Any<string>(), Arg.Any<object>()).Returns(1);

// Act
int result = await repository.SaveAnalyticsData(data);

// Assert
Assert.Equal(1, result);
// ... rest of your assertions
}

}
7 changes: 7 additions & 0 deletions myapp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@
<PackageReference Include="Dapper" Version="2.1.35" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.6" />
<PackageReference Include="Npgsql" Version="8.0.3" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

</Project>

0 comments on commit 0de45f1

Please sign in to comment.