Created
February 1, 2018 16:37
-
-
Save pavel-agarkov/b7a424c88053c8684c3a404b4815e331 to your computer and use it in GitHub Desktop.
Example controller and its tests
This file contains hidden or 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.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"); | |
} | |
} | |
} |
This file contains hidden or 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 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