controller:
[Route("api/[controller]")]
public class ValuesController : Controller
{
[HttpGet]
public IEnumerable<string> Get()
{
return new[] { "value1", "value2" };
}
[HttpGet("{id}")]
public IActionResult Get(int id)
{
if (id < 1)
{
return BadRequest($"Invalid request for id {id}");
}
return Content($"Value {id}");
}
[HttpPost("StartJob")]
public IActionResult StartJob()
{
return Ok("Batch Job Started");
}
}
Unit Test:
public class ValuesControllerShould
{
[Fact]
public void ReturnValues() {
var sut = new ValuesController();
string[] result = sut.Get().ToArray();
Assert.Equal(2, result.Length);
Assert.Equal("value1", result[0]);
Assert.Equal("value2", result[1]);
}
[Fact]
public void ReturnBadRequest() {
var sut = new ValuesController();
IActionResult result = sut.Get(0);
var badRequestResult = Assert.IsType<BadRequestObjectResult>(result);
Assert.Equal("Invalid request for id 0", badRequestResult.Value);
}
[Fact]
public void ReturnContent() {
var sut = new ValuesController();
IActionResult result = sut.Get(1);
var content = Assert.IsType<ContentResult>(result);
Assert.Equal("Value 1", content.Content);
}
[Fact]
public void StartJobOk() {
var sut = new ValuesController();
IActionResult result = sut.StartJob();
var okResult = Assert.IsType<OkObjectResult>(result);
Assert.Equal("Batch Job Started", okResult.Value);
}
}