Skip to content

Commit c4d1f38

Browse files
Add unit test project and exclude from main build
1 parent 1af7e2f commit c4d1f38

3 files changed

Lines changed: 63 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Xunit;
2+
using Microsoft.EntityFrameworkCore;
3+
using Todo.Services;
4+
using Todo.Data;
5+
using Todo.Dal;
6+
using Todo.Models;
7+
using Todo.Domain;
8+
using Microsoft.AspNetCore.Mvc;
9+
using System.Linq;
10+
11+
namespace Todo.Tests.Services
12+
{
13+
public class TaskManagerServicesTests
14+
{
15+
private AppDbContext GetContext()
16+
{
17+
var options = new DbContextOptionsBuilder<AppDbContext>()
18+
.UseInMemoryDatabase(databaseName: System.Guid.NewGuid().ToString())
19+
.Options;
20+
return new AppDbContext(options);
21+
}
22+
23+
[Fact]
24+
public void InsertTask_Should_Add_Task_And_Return_Ok()
25+
{
26+
using var context = GetContext();
27+
context.CategorieTasks.Add(new CategorieTaskEntity { Id = 1, Name = "Test" });
28+
context.SaveChanges();
29+
30+
var taskDal = new TaskDal(context);
31+
var service = new TaskManagerServices(context, taskDal);
32+
var model = new TaskModel { Title = "Sample", Description = "Desc", CategorieTaskId = 1 };
33+
34+
var result = service.InsertTask(model, 1);
35+
36+
var okResult = Assert.IsType<OkObjectResult>(result);
37+
Assert.Equal(1, context.Tasks.Count());
38+
var task = context.Tasks.First();
39+
Assert.Equal("Sample", task.Title);
40+
}
41+
}
42+
}

Todo.Tests/Todo.Tests.csproj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net8.0</TargetFramework>
4+
<IsPackable>false</IsPackable>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
8+
<PackageReference Include="xunit" Version="2.9.2" />
9+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
10+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
11+
<PrivateAssets>all</PrivateAssets>
12+
</PackageReference>
13+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.13" />
14+
</ItemGroup>
15+
<ItemGroup>
16+
<ProjectReference Include="..\Todo.csproj" />
17+
</ItemGroup>
18+
</Project>

Todo.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,8 @@
1919
<PackageReference Include="System.Data.SqlClient" Version="4.8.5" />
2020
<PackageReference Include="xunit.extensibility.core" Version="2.9.2" />
2121
</ItemGroup>
22+
<ItemGroup>
23+
<Compile Remove="Todo.Tests\**\*.cs" />
24+
</ItemGroup>
2225

2326
</Project>

0 commit comments

Comments
 (0)