コード例 #1
0
        public void AddCustomer()
        {
            //Arrange
            var repository = CreateRepository();
            var customer = new Customer() { FirstName = "Larry", LastName = "Hughes" };

            //Act
            repository.AddCustomer(customer);

            //Assert
            mockCustomerSet.Verify(c => c.Add(It.IsAny<Customer>()));
            dbContext.Verify(c => c.SaveChanges());
        }
コード例 #2
0
 /// <summary>
 /// Add a customer to the repository.
 /// </summary>
 /// <param name="customer">Customer object to add to the repository.</param>
 /// <returns></returns>
 public void AddCustomer(Customer customer)
 {
     DBContext.Customers.Add(customer);
     DBContext.SaveChanges();
 }
コード例 #3
0
 /// <summary>
 /// Update a Customer in the repository that has the matching Id with modified property values.
 /// </summary>
 /// <param name="customer">Customer object that contains the modified property values to update with.</param>
 /// <returns></returns>
 public void UpdateCustomer(Customer customer)
 {
     DBContext.Customers.AddOrUpdate(customer);
     DBContext.SaveChanges();
 }
コード例 #4
0
        public void SetupDbContext()
        {
            dbContext = new Mock<IDbContext>();

            mason = new Customer { Id = 2, FirstName = "Mason", LastName = "McGlothlin", Address = "1 Washington Way", CompanyName = "Acme Inc", EmailAddress = "*****@*****.**", PhoneNumber = "555-555-5555" };

            var sampleCustomerData = new List<Customer>
            {
                new Customer { Id = 1, FirstName = "Road", LastName = "Runner", Address = "2 Test Ave", CompanyName = "Stuff Inc", EmailAddress = "*****@*****.**", PhoneNumber = "817-555-5555" },
                mason,
                new Customer { Id = 3, FirstName = "Bobby", LastName = "Tables", Address = "XKCD Street", CompanyName = "Funny Stuff LLC", EmailAddress = "*****@*****.**", PhoneNumber = "214-555-5555" },
            }.AsQueryable();

            mockCustomerSet = new Mock<DbSet<Customer>>();
            mockCustomerSet.As<IQueryable<Customer>>().Setup(m => m.Provider).Returns(sampleCustomerData.Provider);
            mockCustomerSet.As<IQueryable<Customer>>().Setup(m => m.Expression).Returns(sampleCustomerData.Expression);
            mockCustomerSet.As<IQueryable<Customer>>().Setup(m => m.ElementType).Returns(sampleCustomerData.ElementType);
            mockCustomerSet.As<IQueryable<Customer>>().Setup(m => m.GetEnumerator()).Returns(sampleCustomerData.GetEnumerator());
            mockCustomerSet.Setup(c => c.Add(It.IsAny<Customer>())).Returns(new Customer()).Verifiable();
            dbContext.Setup(c => c.Customers).Returns(mockCustomerSet.Object);
            dbContext.Setup(c => c.SaveChanges()).Verifiable();
        }
コード例 #5
0
 /// <summary>
 /// Action method where new customers are posted against.
 /// </summary>
 /// <param name="customer">Model representing new customer to be added to the repository.</param>
 /// <returns>A redirect to the Index action.</returns>
 public ActionResult Create(Customer customer)
 {
     Repository.AddCustomer(customer);
     return RedirectToAction(nameof(CustomersController.Index));
 }
コード例 #6
0
 public void AddCustomer(Customer customer)
 {
     customer.Id = randomNumberGenerator.Next();
     Customers.Add(customer.Id, customer);
 }
コード例 #7
0
 public void UpdateCustomer(Customer customer)
 {
     Customers[customer.Id] = customer;
 }