using Microsoft.EntityFrameworkCore; using ReserveManagementSystemApi.Services; using ReserveManagementSystemLib.Models; using ReserveManagementSystemLib.Persistence; using ReserveManagementSystemLib.Repositories; using System; using System.Collections.Generic; using System.Linq; using System.Text; using TechTalk.SpecFlow; using TechTalk.SpecFlow.Assist; using TechTalk.SpecFlow.Assist.ValueRetrievers; namespace ReserveManagementSystemApi.Tests.Features { [Binding] public sealed class DealerServiceSteps { // For additional details on SpecFlow step definitions see https://go.specflow.org/doc-stepdef private readonly ScenarioContext _scenarioContext; private DealerService _dealerService; private AppDbContext _appDbContext; private IEnumerable _dealersResult; private List _loans; public DealerServiceSteps(ScenarioContext scenarioContext) { _scenarioContext = scenarioContext; Service.Instance.ValueRetrievers.Register(new NullValueRetriever("")); } [BeforeScenario] public void CreateDatabase() { var options = new DbContextOptionsBuilder() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; _appDbContext = new AppDbContext(options); _dealerService = new DealerService(new DealerRepository(_appDbContext)); } [AfterScenario] public void Dispose() { _appDbContext.Database.EnsureDeleted(); _appDbContext.Dispose(); } [Given(@"the dealer table looks like")] public void GivenTheDealerTableLooksLike(Table table) { var dealers = table.CreateSet().ToList(); _appDbContext.Dealers.AddRange(dealers); _appDbContext.SaveChanges(); } [Given(@"a list of loans with dealers that look like")] public void GivenAListOfLoansWithDealersThatLookLike(Table table) { _loans = table.CreateSet().ToList(); var dealers = table.CreateSet().ToList(); for (var i=0; i < _loans.Count; i++) { _loans.Skip(i).First().Dealer = dealers.Skip(i).First(); } } [When(@"I search for dealers with search string '(.*)'")] public async void WhenISearchForDealersWithSearchString(string dealerSearchText) { _dealersResult = await _dealerService.Search(dealerSearchText); } [When(@"I update the dealers from the loans")] public async void WhenIUpdateTheDealersFromTheLoans() { await _dealerService.UpdateDealersAsync(_loans); } [Then(@"dealers returned should be")] public void ThenDealersReturnedShouldBe(Table table) { table.CompareToSet(_dealersResult); } [Then(@"the dealer table now looks like")] public void ThenTheDealerTableNowLooksLike(Table table) { var dealers = _appDbContext.Dealers.ToList(); table.CompareToSet(dealers); } } }