diff --git a/XUnitDIStudy.Test/ControllerTest.cs b/XUnitDIStudy.Test/ControllerTest.cs new file mode 100644 index 0000000..6cfdbb0 --- /dev/null +++ b/XUnitDIStudy.Test/ControllerTest.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.AspNetCore.Mvc.Testing.Handlers; +using Microsoft.AspNetCore.TestHost; + +using Xunit; + +using XUnitDIStudy.Model; + +namespace XUnitDIStudy.Test +{ + public class ControllerTest:IClassFixture> + { + private readonly WebApplicationFactory _factory; + + public ControllerTest(WebApplicationFactory factory) + { + _factory = factory; + } + + + [Fact] + public async Task Test() + { + // Arrange + var client = _factory.CreateClient(); + + // Act + var response = await client.GetAsync("/Default/GetAll"); + + // Assert + response.EnsureSuccessStatusCode(); // Status Code 200-299 + + List result = System.Text.Json.JsonSerializer.Deserialize>(response.Content.ReadAsStringAsync().Result); + + Assert.NotNull(result); + Assert.True(result.Count>0); + } + } +} diff --git a/XUnitDIStudy.Test/XUnitDIStudy.Test.csproj b/XUnitDIStudy.Test/XUnitDIStudy.Test.csproj index afd730a..d769e53 100644 --- a/XUnitDIStudy.Test/XUnitDIStudy.Test.csproj +++ b/XUnitDIStudy.Test/XUnitDIStudy.Test.csproj @@ -1,4 +1,4 @@ - + net5.0 @@ -7,7 +7,9 @@ + + diff --git a/XUnitDIStudy.WebApp/Controllers/DefaultController.cs b/XUnitDIStudy.WebApp/Controllers/DefaultController.cs index 6a8f8fe..d11b376 100644 --- a/XUnitDIStudy.WebApp/Controllers/DefaultController.cs +++ b/XUnitDIStudy.WebApp/Controllers/DefaultController.cs @@ -19,9 +19,9 @@ namespace XUnitDIStudy.WebApp.Controllers public class DefaultController : ControllerBase { private readonly ILogger _logger; - private readonly StudentService _studentService; + private readonly IStudentService _studentService; - public DefaultController(ILogger logger, StudentService studentService) + public DefaultController(ILogger logger, IStudentService studentService) { _logger = logger; _studentService = studentService; diff --git a/XUnitDIStudy.WebApp/Startup.cs b/XUnitDIStudy.WebApp/Startup.cs index aff4516..d83756e 100644 --- a/XUnitDIStudy.WebApp/Startup.cs +++ b/XUnitDIStudy.WebApp/Startup.cs @@ -12,6 +12,8 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using XUnitDIStudy.Service; + namespace XUnitDIStudy.WebApp { public class Startup @@ -26,6 +28,9 @@ namespace XUnitDIStudy.WebApp public void ConfigureServices(IServiceCollection services) { services.AddControllers(); + + services.AddScoped(); + services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "XUnitDIStudy.WebApp", Version = "v1" });