Entity Framework Repository Test Example

Since we are using the Entity Framework’s context object as dependency we should consider this test as an integration test (unless we want to mock EF). Here is the test code:

public class PasswordEntryRepositoryTest
{
    [TestFixture]
    public class When_Password_Entry_Repository_Is_Present
    {
        private DbContext _context;
        private DbContextTransaction _transaction;
        private IRepository<PasswordEntry, string> _passwordEntryRepository;
 
        [SetUp]
        public void SetUp()
        {
            _context = new PasswordAPIContext();
            _transaction = _context.Database.BeginTransaction();
            _passwordEntryRepository = new PasswordEntryRepository(_context);
        }
 
        [Test]
        public void It_Should_Create_A_Password_Entry()
        {
            var item = new PasswordEntry
            {
                UserId = "user",
                Salt = "salt",
                PasswordHash = "passwordHash",
                Expiration = DateTime.Now
            };
            Assert.DoesNotThrow(() => _passwordEntryRepository.AddNew(item));
        }
 
        [Test]
        public void It_Should_Read_All_The_Password_Entries()
        {
            var item = new PasswordEntry
            {
                UserId = "user",
                Salt = "salt",
                PasswordHash = "passwordHash",
                Expiration = DateTime.Now
            };
            _passwordEntryRepository.AddNew(item);
            Assert.IsTrue(_passwordEntryRepository.GetAll().Any());
        }
 
        [Test]
        public void It_Should_Read_A_Password_Entry_By_User()
        {
            var item = new PasswordEntry
            {
                UserId = "user",
                Salt = "salt",
                PasswordHash = "passwordHash",
                Expiration = DateTime.Now
            };
            _passwordEntryRepository.AddNew(item);
            var dbItem = _passwordEntryRepository.GetById("user");
            Assert.AreEqual(item.UserId, dbItem.UserId);
        }
 
        [Test]
        public void It_Should_Update_A_Password_Entry()
        {
            var item = new PasswordEntry
            {
                UserId = "user",
                Salt = "salt",
                PasswordHash = "passwordHash",
                Expiration = DateTime.Now
            };
            _passwordEntryRepository.AddNew(item);
            item.Salt = "saltUpdated";
            _passwordEntryRepository.Update(item);
            var dbItem = _passwordEntryRepository.GetById("user");
            Assert.AreEqual(item.Salt, dbItem.Salt);
        }
 
        [Test]
        public void It_Should_Delete_A_Password_Entry()
        {
            var item = new PasswordEntry
            {
                UserId = "user",
                Salt = "salt",
                PasswordHash = "passwordHash",
                Expiration = DateTime.Now
            };
            _passwordEntryRepository.AddNew(item);
            _passwordEntryRepository.Delete(item.UserId);
            var dbItem = _passwordEntryRepository.GetById("user");
            Assert.IsNull(dbItem);
        }
 
        [TearDown]
        public void TearDown()
        {
            _transaction.Rollback();
            _transaction.Dispose();
            _context.Dispose();
        }
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s