Add tests for controller. Put in contigency for when an item isn't found.

This commit is contained in:
Robert Marshall 2020-04-10 10:01:27 +01:00
parent a22f8b125c
commit c0c1311a0a
6 changed files with 119 additions and 11 deletions

View file

@ -0,0 +1,52 @@
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using Robware.Api.Blog.Controllers;
using Robware.Blog;
using Robware.Data;
using Xunit;
namespace Robware.Api.Blog.Tests {
public class BlogControllerTests {
[Fact]
public async Task Get_WithUrl_ReturnsBlogPost() {
var logger = Substitute.For<ILogger<BlogController>>();
var repo = Substitute.For<IBlogRepository>();
repo.GetPostByUrlAsync("url").Returns(new BlogPost());
var expectation = new BlogPost();
var controller = new BlogController(logger, repo);
(await controller.Get("url")).Value.Should().BeEquivalentTo(expectation);
}
[Fact]
public async Task Get_WithId_ReturnsBlogPost() {
var logger = Substitute.For<ILogger<BlogController>>();
var repo = Substitute.For<IBlogRepository>();
repo.GetPostByIdAsync(1).Returns(new BlogPost());
var expectation = new BlogPost();
var controller = new BlogController(logger, repo);
(await controller.Get("1")).Value.Should().BeEquivalentTo(expectation);
}
[Fact]
public async Task Get_WithInvalidUrl_Returns404() {
var logger = Substitute.For<ILogger<BlogController>>();
var repo = Substitute.For<IBlogRepository>();
repo.GetPostByUrlAsync("url").Throws(new ItemNotFoundException("", null));
var controller = new BlogController(logger, repo);
(await controller.Get("url")).Result.Should().BeOfType<NotFoundObjectResult>();
}
}
}

View file

@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="NSubstitute" Version="4.2.1" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Robware.Api.Blog\Robware.Api.Blog.csproj" />
</ItemGroup>
</Project>