Last active
August 29, 2015 14:15
-
-
Save tarasn/1a21e7d1db19b2ee79c0 to your computer and use it in GitHub Desktop.
Moqing repository methods like IEnumerable<Person> Find(IEnumerable<Person> persons, Expression<Func<Person, bool>> criteria) or IEnumerable<Person> Find(IEnumerable<Person> persons, Func<Person, bool> criteria, bool enabled) using Moq and NUnit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Diagnostics; | |
| using System.Linq; | |
| using System.Linq.Expressions; | |
| using System.Text; | |
| namespace ClassLibrary1 | |
| { | |
| [DebuggerDisplay("FirstName={FirstName}")] | |
| public class Person | |
| { | |
| public string FirstName { get; set; } | |
| public string LastName { get; set; } | |
| public int Age { get; set; } | |
| public bool HasChildren { get; set; } | |
| public DateTime Birthdate { get; set; } | |
| } | |
| public class Repo | |
| { | |
| private readonly IEnumerable<Person> _persons; | |
| private readonly IFinder _finder; | |
| public Repo(IEnumerable<Person> persons, IFinder finder) | |
| { | |
| _persons = persons; | |
| _finder = finder; | |
| } | |
| public IEnumerable<Person> FindContains(string subString) | |
| { | |
| return _finder.Find(_persons, p => p.FirstName.Contains(subString)); | |
| } | |
| public IEnumerable<Person> FindStarts(string prefix) | |
| { | |
| return _finder.Find(_persons, p => p.FirstName.StartsWith(prefix)); | |
| } | |
| public IEnumerable<Person> FindEndss(string sufix) | |
| { | |
| return _finder.Find(_persons, p => p.FirstName.EndsWith(sufix), false); | |
| } | |
| } | |
| public interface IFinder | |
| { | |
| IEnumerable<Person> Find(IEnumerable<Person> persons, Expression<Func<Person, bool>> criteria); | |
| IEnumerable<Person> Find(IEnumerable<Person> persons, Func<Person, bool> criteria, bool enabled); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Linq.Expressions; | |
| using System.Text; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using Moq; | |
| using NUnit.Framework; | |
| namespace ClassLibrary1 | |
| { | |
| [TestFixture] | |
| public class RepoShould | |
| { | |
| private Repo _repo; | |
| private Mock<IFinder> _finderMoq; | |
| [SetUp] | |
| public void Setup() | |
| { | |
| _finderMoq = new Mock<IFinder>(); | |
| var persons = GetPersons(); | |
| _repo = new Repo(persons, _finderMoq.Object); | |
| } | |
| private IEnumerable<Person> GetPersons() | |
| { | |
| return new List<Person> | |
| { | |
| new Person | |
| { | |
| FirstName = "Taras", | |
| Age = 41 | |
| }, | |
| new Person | |
| { | |
| FirstName = "Vasya", | |
| Age = 33 | |
| }, | |
| new Person | |
| { | |
| FirstName = "Dima", | |
| Age = 17 | |
| } | |
| }; | |
| } | |
| [Test] | |
| public void FindContainsLetter_s() | |
| { | |
| _finderMoq.Setup(f => f.Find(It.IsAny<IEnumerable<Person>>(), It.IsAny<Expression<Func<Person, bool>>>())) | |
| .Returns((IEnumerable<Person> prs, Expression<Func<Person, bool>> criteria) => | |
| { | |
| return GetPersons().Where( criteria.Compile() ); | |
| }); | |
| var persons = _repo.FindContains("s"); | |
| Assert.That(persons.Count(), Is.EqualTo(2)); | |
| } | |
| [Test] | |
| public void FindStartsWithLetter_D() | |
| { | |
| _finderMoq.Setup(f => f.Find(It.IsAny<IEnumerable<Person>>(), It.IsAny<Expression<Func<Person, bool>>>())) | |
| .Returns((IEnumerable<Person> prs, Expression<Func<Person, bool>> criteria) => | |
| { | |
| return GetPersons().Where(criteria.Compile()); | |
| }); | |
| var persons = _repo.FindStarts("D"); | |
| Assert.That(persons.Count(), Is.EqualTo(1)); | |
| } | |
| [Test] | |
| public void FindEndsWithLetter_A() | |
| { | |
| _finderMoq.Setup(f => f.Find(It.IsAny<IEnumerable<Person>>(), It.IsAny<Func<Person, bool>>(), It.IsAny<bool>())) | |
| .Returns((IEnumerable<Person> prs, Func<Person, bool> criteria, bool enabled) => | |
| { | |
| return GetPersons().Where(criteria); | |
| }); | |
| var persons = _repo.FindEndss("s"); | |
| Assert.That(persons.Count(), Is.EqualTo(1)); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //mocking method IEnumerable<T> FindAll(Func<T,bool> criteria); | |
| _storageMoq.Setup(s => s.FindAll(It.IsAny<Func<ConfigurationEntity, bool>>())) | |
| .Returns((Func<ConfigurationEntity, bool> criteria) => ConfigurationEntitiesByCriteria(criteria)); | |
| private IEnumerable<ConfigurationEntity> ConfigurationEntitiesByCriteria(Func<ConfigurationEntity, bool> criteria) | |
| { | |
| var entities = new[] | |
| { | |
| new ConfigurationEntity | |
| { | |
| Key = "username", | |
| Value = "kuku-user", | |
| TypeName = typeof (string).Name | |
| }, | |
| new ConfigurationEntity | |
| { | |
| Key = "password-hash", | |
| Value = Tools.CalculateMD5Hash("kuku-pass"), | |
| TypeName = typeof (string).Name | |
| }, | |
| new ConfigurationEntity | |
| { | |
| Key = "sessionid", | |
| Value = _sessionId.ToString(), | |
| TypeName = typeof (string).Name | |
| }, | |
| }; | |
| return entities.Where(criteria); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment