コード例 #1
0
        public async Task CreateAsync_WithNonExistentUser_ReturnsFalse()
        {
            // Arrange
            var testUserId = Guid.NewGuid().ToString();

            var context = new PoolItDbContext(new DbContextOptionsBuilder <PoolItDbContext>()
                                              .UseInMemoryDatabase(Guid.NewGuid().ToString())
                                              .Options);

            var contactMessagesService = new ContactMessagesService(new EfRepository <ContactMessage>(context), new EfRepository <PoolItUser>(context));

            var serviceModel = new ContactMessageServiceModel
            {
                Email    = "*****@*****.**",
                FullName = "Test Name",
                Subject  = "Test Subject",
                Message  = "Test Message",
                UserId   = testUserId
            };

            // Act
            var result = await contactMessagesService.CreateAsync(serviceModel);

            // Assert
            Assert.False(result);

            var messageExists = await context.ContactMessages.AnyAsync();

            Assert.False(messageExists);
        }
コード例 #2
0
        public async Task CreateAsync_WithoutUser_WorksCorrectly()
        {
            // Arrange
            var context = new PoolItDbContext(new DbContextOptionsBuilder <PoolItDbContext>()
                                              .UseInMemoryDatabase(Guid.NewGuid().ToString())
                                              .Options);

            var contactMessagesService = new ContactMessagesService(new EfRepository <ContactMessage>(context), new EfRepository <PoolItUser>(context));

            var serviceModel = new ContactMessageServiceModel
            {
                Email    = "*****@*****.**",
                FullName = "Test Name",
                Subject  = "Test Subject",
                Message  = "Test Message"
            };

            // Act
            var result = await contactMessagesService.CreateAsync(serviceModel);

            // Assert
            Assert.True(result);

            var dbModel = await context.ContactMessages.SingleOrDefaultAsync();

            Assert.NotNull(dbModel);

            Assert.Equal(serviceModel.Email, dbModel.Email);
            Assert.Equal(serviceModel.FullName, dbModel.FullName);
        }
コード例 #3
0
        public async Task CreateAsync_WithValidModel_WorksCorrectly_WithUnsignedUser()
        {
            var context = new JobFinderDbContext(new DbContextOptionsBuilder <JobFinderDbContext>()
                                                 .UseInMemoryDatabase(Guid.NewGuid().ToString())
                                                 .Options);

            var serviceModel = new ContactMessageServiceModel
            {
                Subject  = "TestSubjectName",
                Message  = "TestMessage",
                FullName = "TestFullName",
                Email    = "*****@*****.**"
            };

            var contactMessagesService = new ContactMessagesService(new EfRepository <ContactMessage>(context),
                                                                    new EfRepository <User>(context));

            var result = await contactMessagesService.CreateAsync(serviceModel);

            Assert.True(result);

            var dbModel = context.ContactMessages.SingleOrDefault();

            Assert.NotNull(dbModel);
        }
コード例 #4
0
        public async Task CreateAsync_WithUser_WorksCorrectly()
        {
            // Arrange
            var context = new PoolItDbContext(new DbContextOptionsBuilder <PoolItDbContext>()
                                              .UseInMemoryDatabase(Guid.NewGuid().ToString())
                                              .Options);

            var user = new PoolItUser
            {
                UserName  = "******",
                Email     = "*****@*****.**",
                FirstName = "Test",
                LastName  = "User"
            };

            await context.Users.AddAsync(user);

            await context.SaveChangesAsync();

            var contactMessagesService = new ContactMessagesService(new EfRepository <ContactMessage>(context), new EfRepository <PoolItUser>(context));

            var serviceModel = new ContactMessageServiceModel
            {
                Email    = "*****@*****.**",
                FullName = "Other Name",
                Subject  = "Test Subject",
                Message  = "Test Message",
                UserId   = user.Id
            };

            // Act
            var result = await contactMessagesService.CreateAsync(serviceModel);

            // Assert
            Assert.True(result);

            var dbModel = await context.ContactMessages.SingleOrDefaultAsync();

            Assert.NotNull(dbModel);

            Assert.Null(dbModel.Email);
            Assert.Null(dbModel.FullName);

            Assert.Equal(user.Id, dbModel.UserId);
        }
コード例 #5
0
        public async Task CreateAsync_WithInvalidModel_ReturnsFalse()
        {
            var context = new JobFinderDbContext(new DbContextOptionsBuilder <JobFinderDbContext>()
                                                 .UseInMemoryDatabase(Guid.NewGuid().ToString())
                                                 .Options);

            var serviceModel = new ContactMessageServiceModel
            {
                Message  = "TestMessage",
                FullName = "TestFullName",
                Email    = "*****@*****.**"
            };

            var contactMessagesService = new ContactMessagesService(new EfRepository <ContactMessage>(context),
                                                                    new EfRepository <User>(context));

            var result = await contactMessagesService.CreateAsync(serviceModel);

            Assert.False(result);

            var dbModels = await context.ContactMessages.AnyAsync();

            Assert.False(dbModels);
        }