-
-
Save sravanithamatamqualbrain/546fe22ff996225995dfc58da0bbb06e to your computer and use it in GitHub Desktop.
ASP.NET Core Integration tests code from my video: https://www.youtube.com/watch?v=7roqteWLw4s
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
using System.Net.Http; | |
using System.Net.Http.Headers; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Mvc.Testing; | |
using Microsoft.EntityFrameworkCore; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.DependencyInjection.Extensions; | |
using Tweetbook.Contracts.V1; | |
using Tweetbook.Contracts.V1.Requests; | |
using Tweetbook.Contracts.V1.Responses; | |
using Tweetbook.Data; | |
namespace Tweetbook.IntegrationTests | |
{ | |
public class IntegrationTest | |
{ | |
protected readonly HttpClient TestClient; | |
protected IntegrationTest() | |
{ | |
var appFactory = new WebApplicationFactory<Startup>() | |
.WithWebHostBuilder(builder => | |
{ | |
builder.ConfigureServices(services => | |
{ | |
services.RemoveAll(typeof(DataContext)); | |
services.AddDbContext<DataContext>(options => { options.UseInMemoryDatabase("TestDb"); }); | |
}); | |
}); | |
TestClient = appFactory.CreateClient(); | |
} | |
protected async Task AuthenticateAsync() | |
{ | |
TestClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", await GetJwtAsync()); | |
} | |
protected async Task<PostResponse> CreatePostAsync(CreatePostRequest request) | |
{ | |
var response = await TestClient.PostAsJsonAsync(ApiRoutes.Posts.Create, request); | |
return await response.Content.ReadAsAsync<PostResponse>(); | |
} | |
private async Task<string> GetJwtAsync() | |
{ | |
var response = await TestClient.PostAsJsonAsync(ApiRoutes.Identity.Register, new UserRegistrationRequest | |
{ | |
Email = "[email protected]", | |
Password = "SomePass1234!" | |
}); | |
var registrationResponse = await response.Content.ReadAsAsync<AuthSuccessResponse>(); | |
return registrationResponse.Token; | |
} | |
} | |
} |
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
using System.Collections.Generic; | |
using System.Net; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using FluentAssertions; | |
using Tweetbook.Contracts.V1; | |
using Tweetbook.Contracts.V1.Requests; | |
using Tweetbook.Domain; | |
using Xunit; | |
namespace Tweetbook.IntegrationTests | |
{ | |
public class PostsControllerTests : IntegrationTest | |
{ | |
[Fact] | |
public async Task GetAll_WithoutAnyPosts_ReturnsEmptyResponse() | |
{ | |
// Arrange | |
await AuthenticateAsync(); | |
// Act | |
var response = await TestClient.GetAsync(ApiRoutes.Posts.GetAll); | |
// Assert | |
response.StatusCode.Should().Be(HttpStatusCode.OK); | |
(await response.Content.ReadAsAsync<List<Post>>()).Should().BeEmpty(); | |
} | |
[Fact] | |
public async Task Get_ReturnsPost_WhenPostExistsInTheDatabase() | |
{ | |
// Arrange | |
await AuthenticateAsync(); | |
var createdPost = await CreatePostAsync(new CreatePostRequest {Name = "Test post"}); | |
// Act | |
var response = await TestClient.GetAsync(ApiRoutes.Posts.Get.Replace("{postId}", createdPost.Id.ToString())); | |
// Assert | |
response.StatusCode.Should().Be(HttpStatusCode.OK); | |
var returnedPost = await response.Content.ReadAsAsync<Post>(); | |
returnedPost.Id.Should().Be(createdPost.Id); | |
returnedPost.Name.Should().Be("Test post"); | |
} | |
} | |
} |
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
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<TargetFramework>netcoreapp2.2</TargetFramework> | |
<IsPackable>false</IsPackable> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="FluentAssertions" Version="5.7.0" /> | |
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.0" /> | |
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.2.0" /> | |
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" /> | |
<PackageReference Include="xunit" Version="2.4.0" /> | |
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" /> | |
</ItemGroup> | |
<ItemGroup> | |
<ProjectReference Include="..\Tweetbook\Tweetbook.csproj" /> | |
</ItemGroup> | |
</Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment