Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9b4462c
commit 43345cd
Showing
13 changed files
with
608 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.TestHost; | ||
using Newtonsoft.Json; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using System.Linq; | ||
using WebApiDemo.Models; | ||
using Xunit; | ||
using FluentAssertions; | ||
using System.Net.Http.Headers; | ||
using System.Text; | ||
using System; | ||
|
||
namespace WebApiDemo.Tests | ||
{ | ||
public class PersonsControllerIntegrationTests | ||
{ | ||
private readonly TestServer _server; | ||
private readonly HttpClient _client; | ||
|
||
public PersonsControllerIntegrationTests() | ||
{ | ||
// Arrange | ||
_server = new TestServer(new WebHostBuilder() | ||
.UseStartup<Startup>()); | ||
_client = _server.CreateClient(); | ||
_client.DefaultRequestHeaders.Accept.Clear(); | ||
_client.DefaultRequestHeaders.Accept.Add( | ||
new MediaTypeWithQualityHeaderValue("application/json")); | ||
} | ||
|
||
[Fact] | ||
public async Task Persons_Get_All() | ||
{ | ||
// Act | ||
var response = await _client.GetAsync("/api/Persons"); | ||
|
||
// Assert | ||
response.EnsureSuccessStatusCode(); | ||
var responseString = await response.Content.ReadAsStringAsync(); | ||
var persons = JsonConvert.DeserializeObject<IEnumerable<Person>>(responseString); | ||
persons.Count().Should().Be(50); | ||
} | ||
|
||
[Fact] | ||
public async Task Persons_Get_Specific() | ||
{ | ||
// Act | ||
var response = await _client.GetAsync("/api/Persons/16"); | ||
|
||
// Assert | ||
response.EnsureSuccessStatusCode(); | ||
var responseString = await response.Content.ReadAsStringAsync(); | ||
var person = JsonConvert.DeserializeObject<Person>(responseString); | ||
person.Id.Should().Be(16); | ||
} | ||
|
||
[Fact] | ||
public async Task Persons_Post_Specific() | ||
{ | ||
// Arrange | ||
var personToAdd = new Person | ||
{ | ||
FirstName = "John", | ||
LastName = "Doe", | ||
Age = 50, | ||
Title = "FooBar", | ||
Phone = "001 123 1234567", | ||
Email = "john.doe@foo.bar" | ||
}; | ||
var content = JsonConvert.SerializeObject(personToAdd); | ||
var stringContent = new StringContent(content, Encoding.UTF8, "application/json"); | ||
|
||
// Act | ||
var response = await _client.PostAsync("/api/Persons", stringContent); | ||
|
||
// Assert | ||
response.EnsureSuccessStatusCode(); | ||
var responseString = await response.Content.ReadAsStringAsync(); | ||
var person = JsonConvert.DeserializeObject<Person>(responseString); | ||
person.Id.Should().Be(51); | ||
} | ||
|
||
[Fact] | ||
public async Task Persons_Post_Specific_Invalid() | ||
{ | ||
// Arrange | ||
var personToAdd = new Person { FirstName = "John" }; | ||
var content = JsonConvert.SerializeObject(personToAdd); | ||
var stringContent = new StringContent(content, Encoding.UTF8, "application/json"); | ||
|
||
// Act | ||
var response = await _client.PostAsync("/api/Persons", stringContent); | ||
|
||
// Assert | ||
response.StatusCode.Should().Be(System.Net.HttpStatusCode.BadRequest); | ||
var responseString = await response.Content.ReadAsStringAsync(); | ||
responseString.Should().Contain("The Email field is required") | ||
.And.Contain("The LastName field is required") | ||
.And.Contain("The Phone field is required"); | ||
} | ||
|
||
[Fact] | ||
public async Task Persons_Put_Specific() | ||
{ | ||
// Arrange | ||
var personToChange = new Person | ||
{ | ||
Id = 16, | ||
FirstName = "John", | ||
LastName = "Doe", | ||
Age = 50, | ||
Title = "FooBar", | ||
Phone = "001 123 1234567", | ||
Email = "john.doe@foo.bar" | ||
}; | ||
var content = JsonConvert.SerializeObject(personToChange); | ||
var stringContent = new StringContent(content, Encoding.UTF8, "application/json"); | ||
|
||
// Act | ||
var response = await _client.PutAsync("/api/Persons/16", stringContent); | ||
|
||
// Assert | ||
response.EnsureSuccessStatusCode(); | ||
var responseString = await response.Content.ReadAsStringAsync(); | ||
responseString.Should().Be(String.Empty); | ||
} | ||
|
||
|
||
[Fact] | ||
public async Task Persons_Put_Specific_Invalid() | ||
{ | ||
// Arrange | ||
var personToChange = new Person { FirstName = "John" }; | ||
var content = JsonConvert.SerializeObject(personToChange); | ||
var stringContent = new StringContent(content, Encoding.UTF8, "application/json"); | ||
|
||
// Act | ||
var response = await _client.PutAsync("/api/Persons/16", stringContent); | ||
|
||
// Assert | ||
response.StatusCode.Should().Be(System.Net.HttpStatusCode.BadRequest); | ||
var responseString = await response.Content.ReadAsStringAsync(); | ||
responseString.Should().Contain("The Email field is required") | ||
.And.Contain("The LastName field is required") | ||
.And.Contain("The Phone field is required"); | ||
} | ||
|
||
[Fact] | ||
public async Task Persons_Delete_Specific() | ||
{ | ||
// Arrange | ||
|
||
// Act | ||
var response = await _client.DeleteAsync("/api/Persons/16"); | ||
|
||
// Assert | ||
response.EnsureSuccessStatusCode(); | ||
var responseString = await response.Content.ReadAsStringAsync(); | ||
responseString.Should().Be(String.Empty); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using System.Linq; | ||
using WebApiDemo.Controllers; | ||
using WebApiDemo.Models; | ||
using Xunit; | ||
using FluentAssertions; | ||
using WebApiDemo.Services; | ||
using System; | ||
|
||
namespace WebApiDemo.Tests | ||
{ | ||
public class PersonsControllerUnitTests | ||
{ | ||
[Fact] | ||
public async Task Persons_Get_All() | ||
{ | ||
// Arrange | ||
var controller = new PersonsController(new PersonService()); | ||
|
||
// Act | ||
var result = await controller.Get(); | ||
|
||
// Assert | ||
var okResult = result.Should().BeOfType<OkObjectResult>().Subject; | ||
var persons = okResult.Value.Should().BeAssignableTo<IEnumerable<Person>>().Subject; | ||
|
||
persons.Count().Should().Be(50); | ||
} | ||
|
||
[Fact] | ||
public async Task Persons_Get_Specific() | ||
{ | ||
// Arrange | ||
var controller = new PersonsController(new PersonService()); | ||
|
||
// Act | ||
var result = await controller.Get(16); | ||
|
||
// Assert | ||
var okResult = result.Should().BeOfType<OkObjectResult>().Subject; | ||
var person = okResult.Value.Should().BeAssignableTo<Person>().Subject; | ||
person.Id.Should().Be(16); | ||
} | ||
|
||
[Fact] | ||
public async Task Persons_Add() | ||
{ | ||
// Arrange | ||
var controller = new PersonsController(new PersonService()); | ||
var newPerson = new Person | ||
{ | ||
FirstName = "John", | ||
LastName = "Doe", | ||
Age = 50, | ||
Title = "FooBar", | ||
Email = "john.doe@foo.bar" | ||
}; | ||
|
||
// Act | ||
var result = await controller.Post(newPerson); | ||
|
||
// Assert | ||
var okResult = result.Should().BeOfType<CreatedAtActionResult>().Subject; | ||
var person = okResult.Value.Should().BeAssignableTo<Person>().Subject; | ||
person.Id.Should().Be(51); | ||
} | ||
|
||
[Fact] | ||
public async Task Persons_Change() | ||
{ | ||
// Arrange | ||
var service = new PersonService(); | ||
var controller = new PersonsController(service); | ||
var newPerson = new Person | ||
{ | ||
FirstName = "John", | ||
LastName = "Doe", | ||
Age = 50, | ||
Title = "FooBar", | ||
Email = "john.doe@foo.bar" | ||
}; | ||
|
||
// Act | ||
var result = await controller.Put(20, newPerson); | ||
|
||
// Assert | ||
var okResult = result.Should().BeOfType<NoContentResult>().Subject; | ||
|
||
var person = service.Get(20); | ||
person.Id.Should().Be(20); | ||
person.FirstName.Should().Be("John"); | ||
person.LastName.Should().Be("Doe"); | ||
person.Age.Should().Be(50); | ||
person.Title.Should().Be("FooBar"); | ||
person.Email.Should().Be("john.doe@foo.bar"); | ||
} | ||
|
||
[Fact] | ||
public async Task Persons_Delete() | ||
{ | ||
// Arrange | ||
var service = new PersonService(); | ||
var controller = new PersonsController(service); | ||
|
||
// Act | ||
var result = await controller.Delete(20); | ||
|
||
// Assert | ||
var okResult = result.Should().BeOfType<NoContentResult>().Subject; | ||
// should throw an eception, | ||
// because the person with id==20 doesn't exist enymore | ||
AssertionExtensions.ShouldThrow<InvalidOperationException>( | ||
() => service.Get(20)); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.0</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" /> | ||
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.0.0" /> | ||
<PackageReference Include="FluentAssertions" Version="4.19.2" /> | ||
<PackageReference Include="Moq" Version="4.7.63" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" /> | ||
<PackageReference Include="xunit" Version="2.2.0" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\WebApiDemo\WebApiDemo.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio 15 | ||
VisualStudioVersion = 15.0.26730.12 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApiDemo.Tests", "WebApiDemo.Tests\WebApiDemo.Tests.csproj", "{7A3EC1D8-40D7-44E8-857F-E46E2514BF18}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApiDemo", "WebApiDemo\WebApiDemo.csproj", "{54457D7B-646A-4F9F-BBDA-7767A8D91FAD}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{7A3EC1D8-40D7-44E8-857F-E46E2514BF18}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{7A3EC1D8-40D7-44E8-857F-E46E2514BF18}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{7A3EC1D8-40D7-44E8-857F-E46E2514BF18}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{7A3EC1D8-40D7-44E8-857F-E46E2514BF18}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{54457D7B-646A-4F9F-BBDA-7767A8D91FAD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{54457D7B-646A-4F9F-BBDA-7767A8D91FAD}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{54457D7B-646A-4F9F-BBDA-7767A8D91FAD}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{54457D7B-646A-4F9F-BBDA-7767A8D91FAD}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {20142D6B-B2F3-42F6-A7A0-9F0DBF369745} | ||
EndGlobalSection | ||
EndGlobal |
Oops, something went wrong.