Adding unit tests
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
using GameAuthApi.Controllers;
|
||||
using GameAuthApi.Models;
|
||||
using GameAuthApi.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
public class UserControllerTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task GetAllUsers_ReturnsOk()
|
||||
{
|
||||
var users = new List<User>{ new User{ Username="u" } };
|
||||
var config = new ConfigurationBuilder()
|
||||
.AddInMemoryCollection(new Dictionary<string,string>
|
||||
{
|
||||
{"MongoDB:ConnectionString","mongodb://localhost"},
|
||||
{"MongoDB:DatabaseName","test"}
|
||||
}).Build();
|
||||
var mockService = new Mock<UserService>(config);
|
||||
mockService.Setup(s => s.GetAllAsync()).ReturnsAsync(users);
|
||||
var controller = new UserController(mockService.Object);
|
||||
|
||||
var result = await controller.GetAllUsers();
|
||||
|
||||
var ok = Assert.IsType<OkObjectResult>(result);
|
||||
Assert.Equal(users, ok.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteUser_NotFound()
|
||||
{
|
||||
var config = new ConfigurationBuilder()
|
||||
.AddInMemoryCollection(new Dictionary<string,string>
|
||||
{
|
||||
{"MongoDB:ConnectionString","mongodb://localhost"},
|
||||
{"MongoDB:DatabaseName","test"}
|
||||
}).Build();
|
||||
var mockService = new Mock<UserService>(config);
|
||||
mockService.Setup(s => s.DeleteAsync("no")).ReturnsAsync(false);
|
||||
var controller = new UserController(mockService.Object);
|
||||
|
||||
var result = await controller.DeleteUser("no");
|
||||
|
||||
Assert.IsType<NotFoundObjectResult>(result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user