Esempio n. 1
0
        public async Task CreatePersonAsync_MapsToEntityAndAddsEntityAndSavesChanges()
        {
            var personDto            = _fixture.Create <Domain.Dto.Person>();
            var expectedPersonEntity = _fixture.Create <Entities.Person>();

            var personList = _fixture.CreateMany <Entities.Person>().ToList();
            var peopleMock = new Mock <DbSet <Entities.Person> >();

            DbSetMockHelper.SetupMockDbSet(personList.AsQueryable(), peopleMock);
            peopleMock.Setup(p => p.AddAsync(expectedPersonEntity, default(CancellationToken)))
            .ReturnsAsync((EntityEntry <Entities.Person>)null)
            .Verifiable();

            var peopleSearchContextMock = new Mock <IPeopleSearchContext>();

            peopleSearchContextMock.Setup(c => c.People).Returns(peopleMock.Object).Verifiable();
            peopleSearchContextMock.Setup(c => c.SaveChangesAsync(default(CancellationToken)))
            .ReturnsAsync(2)
            .Verifiable();

            var mapperMock = new Mock <IMapper>();

            mapperMock.Setup(m => m.Map <Entities.Person>(personDto)).Returns(expectedPersonEntity)
            .Verifiable();

            var sut = new PersonRepository(peopleSearchContextMock.Object, mapperMock.Object);

            await sut.CreatePersonAsync(personDto);

            mapperMock.Verify();
            peopleMock.Verify();
            peopleSearchContextMock.Verify();
        }
Esempio n. 2
0
        public async Task SearchByNameAsync_WhenSearchTextIsFromLastName_ThenFiltersByLastNameAndMapsToDto()
        {
            var personList = _fixture.CreateMany <Entities.Person>().ToList();
            var peopleMock = new Mock <DbSet <Entities.Person> >();

            DbSetMockHelper.SetupMockDbSet(personList.AsQueryable(), peopleMock);

            var peopleSearchContextMock = new Mock <IPeopleSearchContext>();

            peopleSearchContextMock.Setup(c => c.People).Returns(peopleMock.Object).Verifiable();
            var mapperMock = new Mock <IMapper>();

            mapperMock.Setup(m => m.Map <Domain.Dto.Person>(It.IsAny <Entities.Person>())).Returns <Entities.Person>(p => new Domain.Dto.Person
            {
                FirstName = p.FirstName
            }).Verifiable();

            var sut            = new PersonRepository(peopleSearchContextMock.Object, mapperMock.Object);
            var expectedPerson = personList.First();

            var result = (await sut.SearchByNameAsync(expectedPerson.LastName.Substring(0, 5))).ToList();

            peopleSearchContextMock.Verify();
            mapperMock.Verify();
            result.First().FirstName.Should().BeEquivalentTo(expectedPerson.FirstName);
            result.Count.Should().Be(1);
        }
        private Mock <IHellolingoEntities> GetEntitiesMockForEmailValidationTests(List <User> userData)
        {
            var entitiesMock = new Mock <IHellolingoEntities>();
            var usersMock    = DbSetMockHelper.GetDbSetMock(userData);

            usersMock.Setup(u => u.FindAsync(It.IsAny <int>())).ReturnsAsync(userData.FirstOrDefault());
            entitiesMock.Setup(e => e.Users).Returns(usersMock.Object);
            return(entitiesMock);
        }
        private Mock <BlogContext> SetupContext(List <ApplicationUser> users)
        {
            var queryableUsers = users.AsQueryable();

            var usersDbSet = DbSetMockHelper.Get(queryableUsers).Object;

            var blogContext = new Mock <BlogContext>();

            blogContext
            .SetupGet(x => x.Users)
            .Returns(usersDbSet);


            return(blogContext);
        }
        public void TestInitialize()
        {
            var categories = new List <CategoryEntity>()
            {
                new CategoryEntity()
                {
                    Id                      = 1,
                    Name                    = "Laptops",
                    Description             = "Shop Laptops and find popular brands. Save money.",
                    ProductCategoryEntities = new List <ProductCategoryEntity>()
                },
                new CategoryEntity()
                {
                    Id                      = 2,
                    Name                    = "Printers",
                    Description             = "The Best Printers for 2020.",
                    ProductCategoryEntities = new List <ProductCategoryEntity>()
                },
                new CategoryEntity()
                {
                    Id                      = 3,
                    Name                    = "Sale",
                    Description             = "Shop all sale items",
                    ProductCategoryEntities = new List <ProductCategoryEntity>()
                }
            };

            var mock = new Mock <IDbContext>();

            var categoriesDbSet = DbSetMockHelper.GetQueryableMockDbSet(categories);

            mock.Setup(d => d.GetDbSet <CategoryEntity>()).Returns(categoriesDbSet);

            InjectService(mock.Object);
            InjectService <IUnitOfWorkContext>(mock.Object);

            ConfigureServices();

            Manager   = ServiceProvider.GetRequiredService <ICategoryManager>();
            Converter = ServiceProvider.GetRequiredService <IConverter <CategoryDto, CategoryModel> >();
        }
Esempio n. 6
0
        public void TestInitialize()
        {
            var products = new List <ProductEntity>()
            {
                new ProductEntity()
                {
                    Id                      = 1,
                    Name                    = "HP 410",
                    Description             = "All-in-One Wireless Ink Tank Color Printer",
                    Price                   = 90,
                    AvailableCount          = 9,
                    ProductCategoryEntities = new List <ProductCategoryEntity>()
                },
                new ProductEntity()
                {
                    Id                      = 2,
                    Name                    = "Epson L3152",
                    Description             = "WiFi All in One Ink Tank Printer",
                    Price                   = 60,
                    AvailableCount          = 19,
                    ProductCategoryEntities = new List <ProductCategoryEntity>()
                }
            };

            var categories = new List <CategoryEntity>()
            {
                new CategoryEntity()
                {
                    Id                      = 1,
                    Name                    = "Laptops",
                    Description             = "Shop Laptops and find popular brands. Save money.",
                    ProductCategoryEntities = new List <ProductCategoryEntity>()
                },
                new CategoryEntity()
                {
                    Id                      = 2,
                    Name                    = "Printers",
                    Description             = "The Best Printers for 2020.",
                    ProductCategoryEntities = new List <ProductCategoryEntity>()
                },
                new CategoryEntity()
                {
                    Id                      = 3,
                    Name                    = "Sale",
                    Description             = "Shop all sale items",
                    ProductCategoryEntities = new List <ProductCategoryEntity>()
                }
            };

            var mock = new Mock <IDbContext>();

            var productsDbSet   = DbSetMockHelper.GetQueryableMockDbSet(products);
            var categoriesDbSet = DbSetMockHelper.GetQueryableMockDbSet(categories);

            mock.Setup(d => d.GetDbSet <ProductEntity>()).Returns(productsDbSet);
            mock.Setup(d => d.GetDbSet <CategoryEntity>()).Returns(categoriesDbSet);

            InjectService(mock.Object);
            InjectService <IUnitOfWorkContext>(mock.Object);

            ConfigureServices();

            Manager   = ServiceProvider.GetRequiredService <IProductManager>();
            Converter = ServiceProvider.GetRequiredService <IConverter <ProductDto, ProductModel> >();
        }
 private Mock <DbSet <User> > GetUsersDbSetMock(IEnumerable <User> usersData)
 {
     return(DbSetMockHelper.GetDbSetMock(usersData));
 }