public void Initialize()
        {
            _users = new List <User>
            {
                new User {
                    UserType = (byte)Models.Enums.AccountType.PowerUser,
                    MailId   = "*****@*****.**",
                    Password = "******",
                    UniqueId = Guid.NewGuid(),
                    Username = "******",
                    Id       = 3
                }
            };

            var mockSet = EntityFrameworkMoqHelper.CreateMockForDbSet <User>()
                          .SetupForQueryOn(_users)
                          .WithAdd(_users, "Id")
                          .WithFind(_users, "Id")
                          .WithRemove(_users);

            _mockContext =
                EntityFrameworkMoqHelper
                .CreateMockForDbContext <MvcBankEntities1, User>(mockSet);

            //AutoMapper.Initialize();
        }
Esempio n. 2
0
        public void Customer_CRUD_operations_basics()
        {
            var customer = new Customer()
            {
                Name = "Foo Bar", Address = "Los Angeles, CA"
            };

            var customers = new List <Customer>();
            var mockSet   = EntityFrameworkMoqHelper.CreateMockForDbSet <Customer>()
                            .SetupForQueryOn(customers)
                            .WithAdd(customers, "CustomerID")                                //overwritten to simulate behavior of auto-increment database
                            .WithFind(customers, "CustomerID")
                            .WithRemove(customers);

            var mockContext = EntityFrameworkMoqHelper.CreateMockForDbContext <DemoContext, Customer>(mockSet);

            var customerService = new CustomerService(mockContext.Object);

            customerService.Insert(customer);

            customers.Should().Contain(x => x.CustomerID == customer.CustomerID);

            //Testing GetByID (and DbSet.Find) method
            customerService.GetByID(customer.CustomerID).Should().NotBeNull();

            //Testing Remove method
            customerService.Remove(customer);

            customerService.GetByID(customer.CustomerID).Should().BeNull();
        }
Esempio n. 3
0
        public void GetAllProducts_ReturnsAnEmptyList_WhenDatabaseIsEmpty()
        {
            //Arrange
            List <Product>    productsInDatabase   = new List <Product>();
            var               mockDbSet            = EntityFrameworkMoqHelper.CreateMockForDbSet <Product>().SetupForQueryOn(productsInDatabase);
            var               mockDatabasePlatform = EntityFrameworkMoqHelper.CreateMockForDbContext <OneDayProjectEntities, Product>(mockDbSet);
            ProductRepository productRepository    = new ProductRepository(mockDatabasePlatform.Object);
            int               expectedResult       = 0;

            //Act
            List <Product> actualResult = productRepository.GetAllProducts();

            //Assert
            Assert.AreEqual(expectedResult, actualResult.Count);
        }
        public void AddUser_AddsUserToDatabase_WhenPassedAUserObject()
        {
            //Arrange
            List <User> usersInDatabase = new List <User>();
            var         mockDbSet       = EntityFrameworkMoqHelper.CreateMockForDbSet <User>()
                                          .SetupForQueryOn(usersInDatabase)
                                          .WithAdd(usersInDatabase);
            var            mockDatabasePlatform = EntityFrameworkMoqHelper.CreateMockForDbContext <OneDayProjectEntities, User>(mockDbSet);
            UserRepository usersRepository      = new UserRepository(mockDatabasePlatform.Object);
            Mock <User>    expectedUser         = new Mock <User>();

            //Act
            usersRepository.AddUser(expectedUser.Object);

            //Assert
            Assert.IsTrue(mockDbSet.Object.Contains(expectedUser.Object));
        }
Esempio n. 5
0
        public void Initialization()
        {
            _maps = new List <Map>()
            {
                new Map
                {
                    Idm       = 0,
                    Name      = "Name0",
                    Latitude  = "56.278206",
                    Longitude = "43.971291",
                    Length    = 100,
                    Width     = 100,
                    Scale     = 1000
                },
                new Map
                {
                    Idm       = 1,
                    Name      = "Name1",
                    Latitude  = "56.278206",
                    Longitude = "43.971291",
                    Length    = 100,
                    Width     = 100,
                    Scale     = 10000
                },
                new Map
                {
                    Idm       = 2,
                    Name      = "Name2",
                    Latitude  = "56.278206",
                    Longitude = "43.971291",
                    Length    = 100,
                    Width     = 100,
                    Scale     = 100000
                },
            };

            var mockSet = EntityFrameworkMoqHelper.CreateMockForDbSet <Map>()
                          .SetupForQueryOn(_maps)
                          .WithAdd(_maps, "Idm")
                          .WithFind(_maps, "Idm")
                          .WithRemove(_maps);

            _mockContext = EntityFrameworkMoqHelper.CreateMockForDbContext <MapGenEntities, Map>(mockSet);
            _mockContext.Setup(c => c.Maps).Returns(mockSet.Object);
        }
Esempio n. 6
0
        public void GetAllUsers_ReturnAnEmptyList_WhenCalled()
        {
            //Arrange
            List <User> listOfUsers = new List <User>();

            var mockDbSet = EntityFrameworkMoqHelper.CreateMockForDbSet <User>()
                            .SetupForQueryOn(listOfUsers);
            var mockContext = EntityFrameworkMoqHelper.CreateMockForDbContext <Context, User>(mockDbSet);

            UserRepository userRepository = new UserRepository(mockContext.Object);
            int            expected       = 0;

            //Act
            List <User> actualresult = userRepository.GetAllUsers();

            //Assert
            Assert.AreEqual(expected, actualresult.Count);
        }
Esempio n. 7
0
        public void GetAllProducts_ReturnsAListOfOne_WhenCalledWithOneProductInDatabase()
        {
            //Arrange
            List <Product> productsInDatabase = new List <Product>();
            Mock <Product> mockUser           = new Mock <Product>();

            productsInDatabase.Add(mockUser.Object);
            var mockDbSet                       = EntityFrameworkMoqHelper.CreateMockForDbSet <Product>().SetupForQueryOn(productsInDatabase);
            var mockDatabasePlatform            = EntityFrameworkMoqHelper.CreateMockForDbContext <OneDayProjectEntities, Product>(mockDbSet);
            ProductRepository productRepository = new ProductRepository(mockDatabasePlatform.Object);
            int expectedResult                  = 1;

            //Act
            List <Product> actualResult = productRepository.GetAllProducts();

            //Assert
            Assert.AreEqual(expectedResult, actualResult.Count);
        }
Esempio n. 8
0
        public void AddNewUser_AddsNewUserToDatabase_WhenPassedInAUserObject()
        {
            //Arrange
            List <User> listOfUsers = new List <User>();
            Mock <User> mockUser    = new Mock <User>();

            var mockDbSet = EntityFrameworkMoqHelper.CreateMockForDbSet <User>()
                            .SetupForQueryOn(listOfUsers)
                            .WithAdd(listOfUsers);
            var            mockContext    = EntityFrameworkMoqHelper.CreateMockForDbContext <Context, User>(mockDbSet);
            UserRepository userRepository = new UserRepository(mockContext.Object);

            //Act
            userRepository.AddNewUser(mockUser.Object);

            //Assert
            Assert.IsTrue(mockDbSet.Object.Contains(mockUser.Object));
        }
Esempio n. 9
0
        public void GetProductById_ReturnsNull_WhenPassedProductIdThatDoesNotExistInDatabase()
        {
            //Arrange
            List <Product> productsInDatabase = new List <Product>();
            int            userId             = 6;
            Mock <Product> mockProduct        = new Mock <Product>();

            productsInDatabase.Add(mockProduct.Object);
            var mockDbSet = EntityFrameworkMoqHelper.CreateMockForDbSet <Product>()
                            .SetupForQueryOn(productsInDatabase);
            var mockDatabasePlatform            = EntityFrameworkMoqHelper.CreateMockForDbContext <OneDayProjectEntities, Product>(mockDbSet);
            ProductRepository productRepository = new ProductRepository(mockDatabasePlatform.Object);

            //Act
            Product actualResult = productRepository.GetProductById(userId);

            //Assert
            Assert.IsNull(actualResult);
        }
Esempio n. 10
0
        public void GetAllUsers_ReturnAListContainingUsersFromDatabase_WhenMethodCalled()
        {
            //Arrange
            List <User> listOfUsers = new List <User>();
            Mock <User> mockUser    = new Mock <User>();

            listOfUsers.Add(mockUser.Object);

            var mockDbSet = EntityFrameworkMoqHelper.CreateMockForDbSet <User>()
                            .SetupForQueryOn(listOfUsers);
            var mockContext = EntityFrameworkMoqHelper.CreateMockForDbContext <Context, User>(mockDbSet);

            UserRepository userRepository = new UserRepository(mockContext.Object);

            //Act
            List <User> actualResult = userRepository.GetAllUsers();

            //Assert
            CollectionAssert.Contains(actualResult, mockUser.Object);
        }
        public void GetUserByEmailAddress_UserFoundByMethodIsTheSameAsUserInDatabase_WhenPassedString()
        {
            //Arrange
            List <User> usersInDatabase = new List <User>();
            string      emailAddress    = "*****@*****.**";
            Mock <User> mockUser        = new Mock <User>();

            mockUser.Object.EmailAddress = emailAddress;
            usersInDatabase.Add(mockUser.Object);
            var mockDbSet = EntityFrameworkMoqHelper.CreateMockForDbSet <User>()
                            .SetupForQueryOn(usersInDatabase);
            var            mockDatabasePlatform = EntityFrameworkMoqHelper.CreateMockForDbContext <OneDayProjectEntities, User>(mockDbSet);
            UserRepository userRepository       = new UserRepository(mockDatabasePlatform.Object);
            User           expectedUser         = usersInDatabase[0];

            //Act
            User actualResult = userRepository.GetUserByEmailAddress(emailAddress);

            //Assert
            Assert.AreEqual(expectedUser, actualResult);
        }
Esempio n. 12
0
        public void Customer_insert_checking_property_customerID()
        {
            var customer = new Customer()
            {
                Name = "Foo Bar", Address = "Los Angeles, CA"
            };

            var customers = new List <Customer>();
            var mockSet   = EntityFrameworkMoqHelper.CreateMockForDbSet <Customer>()
                            .SetupForQueryOn(customers)
                            .WithAdd(customers, "CustomerID");                                //overwritten to simulate behavior of auto-increment database

            var mockContext = EntityFrameworkMoqHelper.CreateMockForDbContext <DemoContext, Customer>(mockSet);

            var customerService = new CustomerService(mockContext.Object);

            customerService.Insert(customer);

            //Instead of checking implementations details, it was checking bussines rules
            Assert.IsTrue(customer.CustomerID > 0);
        }
Esempio n. 13
0
        public void GetProductById_ReturnsProductFromDatabaseWithProductIdMatchingIntPassedIntoTheMethod_WhenPassedAnInt()
        {
            //Arrange
            List <Product> productsInDatabase = new List <Product>();
            int            productId          = 1;
            Mock <Product> mockProduct        = new Mock <Product>();

            mockProduct.Object.ProductId = productId;
            productsInDatabase.Add(mockProduct.Object);
            var mockDbSet = EntityFrameworkMoqHelper.CreateMockForDbSet <Product>()
                            .SetupForQueryOn(productsInDatabase)
                            .WithFind(productsInDatabase, "ProductId");
            var mockDatabasePlatform            = EntityFrameworkMoqHelper.CreateMockForDbContext <OneDayProjectEntities, Product>(mockDbSet);
            ProductRepository productRepository = new ProductRepository(mockDatabasePlatform.Object);

            //Act
            Product actualResult = productRepository.GetProductById(productId);

            //Assert
            Assert.AreSame(mockProduct.Object, actualResult);
        }
        public void AddBasketToUser_AddsUserForeignKeyToBasketTable_WhenBasketObjectIsPassed()
        {
            //Arrange
            List <User> usersInDatabase = new List <User>();
            int         userId          = 1;
            Mock <User> mockUser        = new Mock <User>();

            mockUser.Object.UserId = userId;
            usersInDatabase.Add(mockUser.Object);
            var mockDbSet = EntityFrameworkMoqHelper.CreateMockForDbSet <User>()
                            .SetupForQueryOn(usersInDatabase)
                            .WithFind(usersInDatabase, "UserId");
            var mockDatabasePlatform          = EntityFrameworkMoqHelper.CreateMockForDbContext <OneDayProjectEntities, User>(mockDbSet);
            BasketRepository basketRepository = new BasketRepository(mockDatabasePlatform.Object);
            Mock <Basket>    mockBasket       = new Mock <Basket>();

            //Act
            basketRepository.CreateBasket(userId, mockBasket.Object);

            //Assert
            CollectionAssert.Contains(mockDatabasePlatform.Object.Users.ToList()[0].Baskets, mockBasket.Object);
        }
Esempio n. 15
0
        public void Customer_insert_checking_EF_implementation_details()
        {
            var customer = new Customer()
            {
                Name = "Foo Bar", Address = "Los Angeles, CA"
            };

            var customers = new List <Customer>();
            var mockSet   = EntityFrameworkMoqHelper.CreateMockForDbSet <Customer>()
                            .SetupForQueryOn(customers)
                            .WithAdd(customers);

            var mockContext = EntityFrameworkMoqHelper.CreateMockForDbContext <DemoContext, Customer>(mockSet);

            var customerService = new CustomerService(mockContext.Object);

            customerService.Insert(customer);

            //Checking how many times 'DbSet.Add' and 'DbContext.SaveChanges' was called
            mockSet.Verify(m => m.Add(It.IsAny <Customer>()), Times.Once);
            mockContext.Verify(m => m.SaveChanges(), Times.Once);
        }
Esempio n. 16
0
        public void GetUserByUsername_ReturnUsersFormDatabaseWithUsernameMatchingStringPassedIntoAMethod_WhenPassedUsername()
        {
            //Arrange
            List <User> listOfUsers = new List <User>();
            string      username    = "******";
            Mock <User> mockUser    = new Mock <User>();

            mockUser.Object.Username = username;
            listOfUsers.Add(mockUser.Object);

            var mockDbSet = EntityFrameworkMoqHelper.CreateMockForDbSet <User>()
                            .SetupForQueryOn(listOfUsers)
                            .WithFind(listOfUsers, "Username");
            var            mockContext    = EntityFrameworkMoqHelper.CreateMockForDbContext <Context, User>(mockDbSet);
            UserRepository userRepository = new UserRepository(mockContext.Object);

            //Act
            User actualResult = userRepository.GetUserByUsername(username);

            //Assert
            Assert.AreSame(mockUser.Object, actualResult);
        }
Esempio n. 17
0
        public void Initialization()
        {
            _points = new List <Point>()
            {
                new Point
                {
                    Idp   = 0,
                    X     = 0,
                    Y     = 1,
                    Depth = 1500,
                    Idm   = 0
                },
                new Point
                {
                    Idp   = 1,
                    X     = 1,
                    Y     = 1,
                    Depth = 2000,
                    Idm   = 1
                },
                new Point
                {
                    Idp   = 2,
                    X     = 1,
                    Y     = 2,
                    Depth = 1500,
                    Idm   = 0
                },
            };

            var mockSet = EntityFrameworkMoqHelper.CreateMockForDbSet <Point>()
                          .SetupForQueryOn(_points)
                          .WithAdd(_points, "Idp")
                          .WithFind(_points, "Idp")
                          .WithRemove(_points);

            _mockContext = EntityFrameworkMoqHelper.CreateMockForDbContext <MapGenEntities, Point>(mockSet);
            _mockContext.Setup(c => c.Points).Returns(mockSet.Object);
        }
Esempio n. 18
0
        public void GetAllProducts_ReturnsAListofProductsEqualToThoseInDatabase_WhenCalled()
        {
            //Arrange
            List <Product> productsInDatabase = new List <Product>();
            Mock <Product> mockProduct        = new Mock <Product>();

            productsInDatabase.Add(mockProduct.Object);
            Mock <Product> mockProduct2 = new Mock <Product>();

            productsInDatabase.Add(mockProduct2.Object);
            var mockDbSet                       = EntityFrameworkMoqHelper.CreateMockForDbSet <Product>().SetupForQueryOn(productsInDatabase);
            var mockDatabasePlatform            = EntityFrameworkMoqHelper.CreateMockForDbContext <OneDayProjectEntities, Product>(mockDbSet);
            ProductRepository productRepository = new ProductRepository(mockDatabasePlatform.Object);
            List <Product>    expectedList      = new List <Product>();

            expectedList.Add(mockProduct.Object);
            expectedList.Add(mockProduct2.Object);

            //Act
            List <Product> actualList = productRepository.GetAllProducts();

            //Assert
            CollectionAssert.AreEqual(expectedList, actualList);
        }