x-lab-lbq-technical-test/backend/LeedsBeerQuest.API/LeedsBeerQuest.API.Tests/Data/Services/CategoryServiceTests.cs
2022-10-30 17:46:52 +00:00

47 lines
1.4 KiB
C#

using System;
using System.Linq;
using System.Threading.Tasks;
using LeedsBeerQuest.API.Data.Contexts;
using LeedsBeerQuest.API.Data.Models;
using LeedsBeerQuest.API.Data.Services;
using LeedsBeerQuest.API.Data.Services.Interfaces;
using Microsoft.EntityFrameworkCore;
using NUnit.Framework;
namespace LeedsBeerQuest.API.Tests.Data.Services;
[TestFixture]
public class CategoryServiceTests
{
private LeedsBeerQuestDbContext _context;
private ICategoryService _categoryService;
[SetUp]
public void SetUp()
{
var builder = new DbContextOptionsBuilder<LeedsBeerQuestDbContext>()
.UseInMemoryDatabase($"CategoryServiceTests.{Guid.NewGuid().ToString()}");
_context = new LeedsBeerQuestDbContext(builder.Options);
_categoryService = new CategoryService(_context);
}
[Test]
public async Task GetCategories_ReturnsAllCategories_WhenAllCategoriesArePresentInDatabase()
{
var categoryTestData = await seedCategoryTestData();
var result = await _categoryService.GetCategories();
Assert.AreEqual(categoryTestData.Length, result.Count());
}
private async Task<Category[]> seedCategoryTestData()
{
var categoryTestData = TestData.CategoryTestData;
_context.Categories.AddRange(categoryTestData);
await _context.SaveChangesAsync();
return categoryTestData;
}
}