Beispiel #1
0
        public void Add()
        {
            // Arrange
            FakeDbContext db = new FakeDbContext();

            db.AddSet(TestData.Contacts);
            int newContactId = TestData.Contacts.Select(c => c.ContactId)
                               .DefaultIfEmpty(0).Max() + 1;
            var newContact = new Contact
            {
                ContactId    = newContactId,
                FirstName    = "Anita",
                Surname      = "Bath",
                EmailAddress = "*****@*****.**"
            };

            // Act
            db.Add(newContact);
            var addedContact = db.Find <Contact>(newContactId);

            // Assert
            Assert.AreEqual("Bath", addedContact.Surname,
                            "New contact surname check failed!!");
            Assert.AreEqual(1, db.Added.Count,
                            "New contact count check failed!!");
        }
        public async Task GetContact_NonExistingId()
        {
            // Arrange
            FakeDbContext db = new FakeDbContext();

            db.AddSet(TestData.Contacts);
            var controller = new ContactsController(db);

            // Act
            var result = await controller.GetContact(
                TestData.Contacts.Select(c => c.ContactId)
                .DefaultIfEmpty(0).Max() + 1) as NotFoundResult;

            // Assert
            Assert.IsNotNull(result, "NotFoundResult expected but not found!!");
        }
        public void GetContacts()
        {
            // Arrange
            FakeDbContext db = new FakeDbContext();

            db.AddSet(TestData.Contacts);
            var controller = new ContactsController(db);

            // Act
            var result = controller.GetContacts();

            // Assert
            Assert.IsNotNull(result, "Null result received!!");
            Assert.AreEqual(TestData.Contacts.ToList().Count, result.Count(),
                            "GetContacts record count test failed!!");
        }
        public async Task GetContact_ExistingId()
        {
            // Arrange
            FakeDbContext db = new FakeDbContext();

            db.AddSet(TestData.Contacts);
            var controller = new ContactsController(db);

            // Act
            var result = await controller.GetContact(2) as OkNegotiatedContentResult <Contact>;

            // Assert
            Assert.IsNotNull(result, "Expected contact not found!!");
            Assert.AreEqual(TestData.Contacts.Where(c => c.ContactId == 2)
                            .Select(c => c.EmailAddress).FirstOrDefault(), result.Content.EmailAddress,
                            "GetContact email address comparison failed!!");
        }
Beispiel #5
0
        public void Find_StandardPrimaryKey()
        {
            // Arrange
            FakeDbContext db = new FakeDbContext();

            db.AddSet(TestData.Contacts);

            // Act
            var contact2 = db.Find <Contact>(2);
            var contact1 = db.Find <Contact>(1);

            // Assert
            Assert.AreEqual("*****@*****.**", contact1.EmailAddress,
                            "Find - Contact 1 email address check failed!!");
            Assert.AreEqual("*****@*****.**", contact2.EmailAddress,
                            "Find - Contact 2 email address check failed!!");
        }
Beispiel #6
0
        public void Remove()
        {
            // Arrange
            FakeDbContext db = new FakeDbContext();

            db.AddSet(TestData.Contacts);

            // Act
            var contact2 = db.Find <Contact>(2);

            db.Remove <Contact>(contact2);
            var removedContact = db.Find <Contact>(2);

            // Assert
            Assert.AreEqual(null, removedContact);
            Assert.AreEqual(1, db.Removed.Count);
        }
Beispiel #7
0
        public async Task FindAsync_StandardPrimaryKey()
        {
            // Arrange
            FakeDbContext db = new FakeDbContext();

            db.AddSet(TestData.Contacts);

            // Act
            var taskContact2 = await db.FindAsync <Contact>(2);

            var taskContact1 = await db.FindAsync <Contact>(1);

            // Assert
            Assert.AreEqual("*****@*****.**", taskContact1.EmailAddress,
                            "FindAsync - Contact 1 email address check failed!!");
            Assert.AreEqual("*****@*****.**", taskContact2.EmailAddress,
                            "FindAsync - Contact 2 email address check failed!!");
        }
Beispiel #8
0
        public void Update()
        {
            // Arrange
            FakeDbContext db = new FakeDbContext();

            db.AddSet(TestData.Contacts);

            // Act
            var contact2 = db.Find <Contact>(2);

            contact2.Title = "Brain Surgeon";
            db.Update <Contact>(contact2);
            var updatedContact = db.Find <Contact>(2);

            // Assert
            Assert.AreEqual("Brain Surgeon", updatedContact.Title,
                            "Updated contact title check failed");
            Assert.AreEqual(1, db.Updated.Count,
                            "Updated contact count check failed");
        }