53 lines
1.7 KiB
C#
53 lines
1.7 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Moq;
|
|
using Server.Controllers;
|
|
using Server.Models;
|
|
using Server.Repository;
|
|
|
|
namespace TestServer
|
|
{
|
|
[TestClass]
|
|
public class UnitTestPost
|
|
{
|
|
private readonly Mock<IGameRepository> _mockRepository;
|
|
private readonly GameController _controller;
|
|
private List<Game> _games;
|
|
|
|
public UnitTestPost()
|
|
{
|
|
_mockRepository = new Mock<IGameRepository>();
|
|
_controller = new GameController(_mockRepository.Object);
|
|
|
|
_games = new List<Game>
|
|
{
|
|
new Game { Id = 1, Titel = "Game1", Zustand = Zustand.Neu, Kommentar = "Das ist Game1"},
|
|
new Game { Id = 2, Titel = "Game2", Zustand = Zustand.Gebraucht, Kommentar = "Das ist Game2"},
|
|
new Game { Id = 3, Zustand = Zustand.Gebraucht, Kommentar = "Das ist Game mit PROBLEM"}
|
|
};
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldReturnOkWhenGameIsAdded()
|
|
{
|
|
var json = System.Text.Json.JsonSerializer.Serialize(_games[0]);
|
|
|
|
_mockRepository.Setup(repo => repo.AddGame(It.IsAny<Game>())).Returns(true);
|
|
|
|
var result = _controller.Post(json);
|
|
|
|
Assert.IsInstanceOfType(result, typeof(OkObjectResult));
|
|
Assert.AreEqual("Aktion wurde erfolgreich durchgeführt", result.ToString());
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldReturnBadRequestWhenInvalidData()
|
|
{
|
|
var json = System.Text.Json.JsonSerializer.Serialize(_games[2]);
|
|
|
|
var result = _controller.Post(json);
|
|
|
|
Assert.IsInstanceOfType(result, typeof(BadRequestObjectResult));
|
|
Assert.AreEqual("Ungültige Spieldaten", result.ToString());
|
|
}
|
|
}
|
|
} |