Skip to content

Instantly share code, notes, and snippets.

@pavel-agarkov
Created February 1, 2018 16:37
Show Gist options
  • Save pavel-agarkov/b7a424c88053c8684c3a404b4815e331 to your computer and use it in GitHub Desktop.
Save pavel-agarkov/b7a424c88053c8684c3a404b4815e331 to your computer and use it in GitHub Desktop.
Example controller and its tests
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace WebApp.Controllers
{
[Route("api/[controller]")]
public class ValuesController : Controller
{
// GET api/values
[HttpGet]
public async Task<IEnumerable<string>> Get()
{
return await Task
.FromResult(new string[] { "value1", "value2" }
.AsEnumerable());
}
// GET api/values/5
[HttpGet("{id}")]
public async Task<string> Get(int id)
{
return await Task.FromResult("value");
}
}
}
using System.Collections.Generic;
using Xunit;
namespace WebApp.Controllers
{
public class ValuesControllerSpec : IClassFixture<ValuesController>
{
private readonly ValuesController controller;
public ValuesControllerSpec(ValuesController controller)
{
this.controller = controller;
}
[Fact]
public async void Get_action_should_return_an_array_of_strings()
{
var result = await controller.Get();
Assert.IsAssignableFrom<IEnumerable<string>>(result);
}
[Fact]
public async void Get_by_id_action_should_return_a_value()
{
var result = await controller.Get(123);
Assert.Equal("value", result);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment