示例#1
0
        public void AddPrivateCustomer_AddsLegitPrivateCustomer_ReturnsNewDBPlusCustomer()
        {
            var options = new DbContextOptionsBuilder <HasserisDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            // Insert data into the database context
            ContactInfo contactInfo = new ContactInfo("*****@*****.**", "41126263");
            Address     address     = new Address("Brandstrupsgade 12", "9000", "Aalborg");

            using (var context = new HasserisDbContext(options, true))
            {
                context.Customers.Add(new Private("Christoffer", "Hollensen", address, contactInfo));
                context.SaveChanges();
            }

            // Declare object to add
            Private customerToAdd = new Private("CholleAdded", "Holle", address, contactInfo);

            customerToAdd.ID = 2;

            // Serialize object so it can be used in our controller method
            var customerToAddJson = Newtonsoft.Json.JsonConvert.SerializeObject(customerToAdd);


            // Act
            // Use a clean instance of the context to run the test
            using (var context = new HasserisDbContext(options, true))
            {
                var customerController = new CustomerController(context); // getting access to that database

                // It selects from the same id so it will change the existing object in the database.
                customerController.AddPrivateCustomer(customerToAddJson);

                // Without getting a dependency on the method that retrieves all we will do it manually again.
                Private customer = (Private)context.Customers.FirstOrDefault(c => c.ID == 2);

                Assert.That(customer.Firstname, Is.EqualTo("CholleAdded"));
            }
        }