diff --git a/PokemonTracker.Tests/Mocks/MockPokemonRepository.cs b/PokemonTracker.Tests/Mocks/MockPokemonRepository.cs new file mode 100644 index 0000000..a6819fa --- /dev/null +++ b/PokemonTracker.Tests/Mocks/MockPokemonRepository.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using PokemonTracker.Models; +using PokemonTracker.Repositories.Interfaces; +using System.Linq; + +namespace PokemonTracker.Tests.Mocks +{ + public class MockPokemonRepository : IPokemonRepository + { + private List _allPokemon; + public MockPokemonRepository(List allPokemon) + { + _allPokemon = allPokemon; + } + + public void Delete(Pokemon obj) + { + var pokemon = Find(obj.Id); + _allPokemon.Remove(pokemon); + } + + public Pokemon Find(int id) + { + return _allPokemon.Find(p => p.Id == id); + } + + public IEnumerable GetAll() + { + return _allPokemon; + } + + public void Insert(Pokemon obj) + { + _allPokemon.Add(obj); + } + + public IEnumerable Where(Expression> where) + { + return _allPokemon.AsQueryable().Where(where).AsEnumerable(); + } + } +} diff --git a/PokemonTracker.Tests/PokemonServiceTests.cs b/PokemonTracker.Tests/PokemonServiceTests.cs new file mode 100644 index 0000000..f7eb735 --- /dev/null +++ b/PokemonTracker.Tests/PokemonServiceTests.cs @@ -0,0 +1,72 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using PokemonTracker.Services.Interfaces; +using PokemonTracker.Tests.Mocks; +using PokemonTracker.Models; +using System.Collections.Generic; +using System.Linq; + +namespace PokemonTracker.Tests +{ + [TestClass] + public class PokemonServiceTests + { + public static List pokemonList = new List + { + new Pokemon { Id = 1, Name = "Bulbasaur", Type = PokemonType.GRASS }, + new Pokemon { Id = 2, Name = "Charmander", Type = PokemonType.FIRE }, + new Pokemon { Id = 3, Name = "Squirtle", Type = PokemonType.WATER }, + new Pokemon { Id = 4, Name = "Pikachu", Type = PokemonType.ELECTRIC }, + new Pokemon { Id = 5, Name = "Jigglypuff", Type = PokemonType.FAIRY }, + new Pokemon { Id = 6, Name = "Growlithe", Type = PokemonType.FIRE }, + new Pokemon { Id = 7, Name = "Machamp", Type = PokemonType.FIGHTING }, + new Pokemon { Id = 8, Name = "Magikarp", Type = PokemonType.WATER }, + new Pokemon { Id = 9, Name = "Gyarados", Type = PokemonType.WATER }, + new Pokemon { Id = 10, Name = "Dragonite", Type = PokemonType.DRAGON } + }; + + public static List pokemonWithT = new List + { + new Pokemon { Id = 3, Name = "Squirtle", Type = PokemonType.WATER }, + new Pokemon { Id = 6, Name = "Growlithe", Type = PokemonType.FIRE }, + new Pokemon { Id = 10, Name = "Dragonite", Type = PokemonType.DRAGON } + }; + + private PokemonService GetService() + { + var mockRepo = new MockPokemonRepository(pokemonList); + return new PokemonService(mockRepo); + } + + [TestMethod] + public void TestGetAllPokemon() + { + var service = GetService(); + var allPokemon = service.GetAllPokemon(); + CompareLists(allPokemon.ToList(), pokemonList); + } + + [TestMethod] + public void TestSearchPokemon() + { + var service = GetService(); + var pokemonSearchedByT = service.SearchPokemonForKey("t"); + CompareLists(pokemonSearchedByT.ToList(), pokemonWithT); + } + + private void CompareLists(List actual, List expected) + { + Assert.AreEqual(actual.Count, expected.Count); + + for (var i = 0; i < actual.Count; i++) + { + var a = actual.ElementAt(i); + var e = expected.ElementAt(i); + + Assert.AreEqual(a.Id, e.Id); + Assert.AreEqual(a.Name, e.Name); + Assert.AreEqual(a.Type, e.Type); + } + } + } +} diff --git a/PokemonTracker.Tests/PokemonTracker.Tests.csproj b/PokemonTracker.Tests/PokemonTracker.Tests.csproj index abb2572..4a13a29 100644 --- a/PokemonTracker.Tests/PokemonTracker.Tests.csproj +++ b/PokemonTracker.Tests/PokemonTracker.Tests.csproj @@ -48,7 +48,8 @@ - + + @@ -57,6 +58,9 @@ PokemonTracker + + +