Skip to content

Commit adf3657

Browse files
author
feyyazacet
committed
Added integratin test
1 parent 004f772 commit adf3657

File tree

8 files changed

+100
-5
lines changed

8 files changed

+100
-5
lines changed

BasicClean.Api.Integration.Test/BasicClean.Api.Integration.Test.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
<ItemGroup>
1010
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.10" />
11+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.9" />
12+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.2" />
1113
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
1214
<PackageReference Include="Moq" Version="4.15.1" />
1315
<PackageReference Include="xunit" Version="2.4.0" />
@@ -17,6 +19,7 @@
1719

1820
<ItemGroup>
1921
<ProjectReference Include="..\BasicClean.Api\BasicClean.Api.csproj" />
22+
<ProjectReference Include="..\BasicClean.Infrastructure\BasicClean.Infrastructure.csproj" />
2023
</ItemGroup>
2124

2225
</Project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using BasicClean.Api.Integration.Test.Setup;
2+
using BasicClean.Infrastructure;
3+
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.AspNetCore.Mvc.Testing;
5+
using Microsoft.EntityFrameworkCore;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using System;
8+
using System.Collections.Generic;
9+
using System.Linq;
10+
using System.Text;
11+
using System.Threading.Tasks;
12+
13+
namespace BasicClean.Api.Integration.Test.Helpers
14+
{
15+
public class TestStartup<TStartup> :WebApplicationFactory<TStartup> where TStartup:class
16+
{
17+
protected override void ConfigureWebHost(IWebHostBuilder builder)
18+
{
19+
builder.ConfigureServices(services =>
20+
{
21+
22+
//var options=DataMisalignedException
23+
services.AddDbContext<TodoDbContext>(options => options.UseInMemoryDatabase("testMemory"));
24+
25+
var testDb = services.BuildServiceProvider().GetRequiredService<TodoDbContext>();
26+
testDb.Database.EnsureCreated();
27+
SeedData.Todos(testDb);
28+
});
29+
//base.ConfigureWebHost(builder);
30+
}
31+
}
32+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using BasicClean.Core.Enitties;
2+
using BasicClean.Infrastructure;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace BasicClean.Api.Integration.Test.Setup
10+
{
11+
public class SeedData
12+
{
13+
public static void Todos(TodoDbContext todoDbContext)
14+
{
15+
var todo = Todo.Create("First title", "first content");
16+
//todo.ChangeStatus(Core.ValueObjects.TodoStatus.InProgress);
17+
todoDbContext.Todos.Add(todo);
18+
todoDbContext.Todos.Add(Todo.Create("First title", "first content"));
19+
todoDbContext.SaveChanges();
20+
}
21+
22+
}
23+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using BasicClean.Api.Integration.Test.Helpers;
2+
using BasicClean.Core.Dtos;
3+
using Newtonsoft.Json;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Net.Http;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
using Xunit;
11+
12+
namespace BasicClean.Api.Integration.Test
13+
{
14+
public class TodoControllerTest: IClassFixture<TestStartup<Startup>>
15+
{
16+
readonly HttpClient _httpClient;
17+
public TodoControllerTest(TestStartup<Startup> testSturtup)
18+
{
19+
_httpClient = testSturtup.CreateClient();
20+
}
21+
22+
[Fact]
23+
public async Task TodoList_ShoultNotBeEmpty_WhenGetAll()
24+
{
25+
var httpResponse = await _httpClient.GetAsync("api/todos");
26+
var stringResponse = await httpResponse.Content.ReadAsStringAsync();
27+
var response = JsonConvert.DeserializeObject<IEnumerable<TodoItemDto>>(stringResponse);
28+
Assert.True(response.Count() > 0, "The todos count was not greater than 0");
29+
30+
}
31+
}
32+
}

BasicClean.Core/Services/TodoService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public async Task<TodoItemDto> CreteTodo(CreateTodoRequestDto createTodo)
3535
{
3636
var todo = Todo.Create(createTodo.Title, createTodo.Content);
3737
await _todoCommandRepository.AddAsync(todo);
38+
return TodoMap(todo);
39+
}
40+
41+
private TodoItemDto TodoMap(Todo todo)
42+
{
3843
return new TodoItemDto
3944
{
4045
Id = todo.Id,

BasicClean.Infrastructure/Register.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ public static class Register
99
{
1010
public static IServiceCollection AddEntityFramework(this IServiceCollection services, IConfiguration configuration)
1111
{
12-
var connectionString = configuration.GetConnectionString("TodoDb");
13-
services.AddDbContextPool<TodoDbContext>(options => options.UseSqlServer(connectionString));
12+
//var connectionString = configuration.GetConnectionString("TodoDb");
13+
//services.AddDbContextPool<TodoDbContext>(options => options.UseSqlServer(connectionString));
1414
services.AddScoped(typeof(ICommandRepository<,>), typeof(EFCommandRepository<,>));
1515
services.AddScoped(typeof(IQueryRepository<,>), typeof(EFQueryRepository<,>));
16-
services.BuildServiceProvider().GetRequiredService<TodoDbContext>().Database.EnsureCreated();
16+
//services.BuildServiceProvider().GetRequiredService<TodoDbContext>().Database.EnsureCreated();
1717

1818
return services;
1919
}

BasicClean.Infrastructure/TodoDbContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace BasicClean.Infrastructure
88
{
9-
class TodoDbContext : DbContext
9+
public class TodoDbContext : DbContext
1010
{
1111
public TodoDbContext(DbContextOptions<TodoDbContext> options) : base(options)
1212
{ }

BasicClean.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BasicClean.Core.Test", "Bas
1717
EndProject
1818
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Integration", "Integration", "{4683806C-91D2-47B1-B340-A231DF286549}"
1919
EndProject
20-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicClean.Api.Integration.Test", "BasicClean.Api.Integration.Test\BasicClean.Api.Integration.Test.csproj", "{B4E4272C-7ACB-4785-AE14-C76ADEDD4E61}"
20+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BasicClean.Api.Integration.Test", "BasicClean.Api.Integration.Test\BasicClean.Api.Integration.Test.csproj", "{B4E4272C-7ACB-4785-AE14-C76ADEDD4E61}"
2121
EndProject
2222
Global
2323
GlobalSection(SolutionConfigurationPlatforms) = preSolution

0 commit comments

Comments
 (0)