Exemple #1
0
        public void ReturnCorrectResult_WhenPaginated(int currentPage,
                                                      int expectedPageSize,
                                                      int expectedTotalCount)
        {
            var breweryComparer =
                Comparer <IBrewery> .Create(((a, b) => string.Compare(a.Name, b.Name, StringComparison.Ordinal)));

            var breweries = new List <Brewery>();

            for (var i = 0; i < expectedTotalCount; i++)
            {
                breweries.Add(new Brewery()
                {
                    Name = this.Fixture.Create <string>()
                });
            }

            var queryableBreweries = breweries.AsQueryable();
            var repository         = new Mock <IRepository <Brewery> >();

            repository.Setup(r => r.All)
            .Returns(queryableBreweries);
            var beerRepo = new Mock <IRepository <Beer> >();

            var service = new BreweryService(repository.Object, beerRepo.Object);
            var result  = service.GetAll(currentPage, expectedPageSize, (a) => a.Name);

            var actualUsers = result as IBrewery[] ?? result.ToArray();
            var actualCount = actualUsers.Count();

            Assert.GreaterOrEqual(expectedPageSize, actualCount);
            CollectionAssert.IsOrdered(actualUsers, breweryComparer);
        }
Exemple #2
0
        public void CallRepositoryGetAllMethodOnce()
        {
            var repository = new Mock <IRepository <Brewery> >();
            var beerRepo   = new Mock <IRepository <Beer> >();

            var service = new BreweryService(repository.Object, beerRepo.Object);

            service.GetAll();

            repository.Verify(r => r.GetAll(), Times.Once);
        }
Exemple #3
0
        public void ReturnCorrectResult_WhenSearching(int foundPerCriteria,
                                                      string search)
        {
            // Arrange
            var breweries = new List <Brewery>();

            breweries.Add(new Brewery()
            {
                Name = "", Country = ""
            });
            breweries.Add(new Brewery()
            {
                Name = "", Country = ""
            });

            for (var i = 0; i < foundPerCriteria; i++)
            {
                breweries.Add(new Brewery()
                {
                    Name    = this.Fixture.Create <string>() + search + this.Fixture.Create <string>(),
                    Country = this.Fixture.Create <string>()
                });
            }

            for (var i = 0; i < foundPerCriteria; i++)
            {
                breweries.Add(new Brewery()
                {
                    Country = this.Fixture.Create <string>() + search + this.Fixture.Create <string>(),
                    Name    = this.Fixture.Create <string>()
                });
            }

            var queryableBreweries = breweries.AsQueryable();
            var repository         = new Mock <IEfRepository <Brewery> >();

            repository.Setup(r => r.All)
            .Returns(queryableBreweries);
            var beerRepo = new Mock <IEfRepository <Beer> >();

            var service = new BreweryService(repository.Object, beerRepo.Object);

            // Act
            var result = service.GetAll(0, int.MaxValue, (a) => a.Name, search);

            // Assert
            var actual = result as IList <IBrewery> ?? result.ToList();

            Assert.AreEqual(foundPerCriteria * 2, actual.Count);
            CollectionAssert.IsSubsetOf(actual, breweries);
        }
Exemple #4
0
        public void ReturnResultFromRepositoryGetAllMethod()
        {
            var expected   = new List <Brewery>();
            var repository = new Mock <IRepository <Brewery> >();

            repository.Setup(r => r.GetAll())
            .Returns(expected);
            var beerRepo = new Mock <IRepository <Beer> >();

            var service = new BreweryService(repository.Object, beerRepo.Object);

            var actual = service.GetAll();

            Assert.AreSame(expected, actual);
        }