public async Task CreateAsync_WithValidData_ShouldInitiliazeChatRoom() { // Arrange this.InitilaizeMapper(); var context = InMemoryDbContext.Initiliaze(); var messagesRepository = new EfRepository <Message>(context); var chatRoomsRepository = new EfRepository <ChatRoom>(context); var usersRepository = new EfRepository <ApplicationUser>(context); var service = new MessagesService(messagesRepository, chatRoomsRepository, usersRepository); await this.SeedUsers(context); var model = new MessageInputModel { UserOneId = "userId", UserTwoUsername = "******", Text = "test text" }; // Act int expectedCount = context.ChatRooms.Count() + 1; await service.CreateAsync(model); // The chatroom does not exist, so sending the first message, should initiliaze it. int actualCount = context.ChatRooms.Count(); // Assert Assert.Equal(expectedCount, actualCount); }
public async Task CreateMethodShouldAddRightMessageInDatabase() { var options = new DbContextOptionsBuilder <ForumDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var db = new ForumDbContext(options); var dateTimeProvider = new Mock <IDateTimeProvider>(); dateTimeProvider.Setup(dtp => dtp.Now()).Returns(new DateTime(2020, 3, 27)); var messagesService = new MessagesService(db, null, dateTimeProvider.Object); await messagesService.CreateAsync("Test", "123", "321"); var expected = new Message { Id = 1, Content = "Test", AuthorId = "123", ReceiverId = "321", CreatedOn = dateTimeProvider.Object.Now() }; var actual = await db.Messages.FirstOrDefaultAsync(); actual.Should().BeEquivalentTo(expected); }
public async Task CreateAsync_WithValidData_ShouldAddMessageToDatabase() { // Arrange this.InitilaizeMapper(); var context = InMemoryDbContext.Initiliaze(); var messagesRepository = new EfRepository <Message>(context); var chatRoomsRepository = new EfRepository <ChatRoom>(context); var usersRepository = new EfRepository <ApplicationUser>(context); var service = new MessagesService(messagesRepository, chatRoomsRepository, usersRepository); await this.SeedUsers(context); await this.SeedChatRooms(context); var model = new MessageInputModel { UserOneId = "userId", UserTwoUsername = "******", Text = "test text" }; // Act var message = await service.CreateAsync(model); string expectedMessageText = context.Messages.FirstOrDefault().Text; string actualMessageText = message.Text; // Assert Assert.Equal(expectedMessageText, actualMessageText); }
public async Task MessageListShouldIncreaseWhenMultipleMessagesAreSent() { var list = new List <Message>(); var mockRepository = new Mock <IDeletableEntityRepository <Message> >(); mockRepository .Setup(x => x.All()) .Returns(list.AsQueryable()); mockRepository .Setup(x => x.AddAsync(It.IsAny <Message>())) .Callback((Message message) => list.Add(message)); var service = new MessagesService(mockRepository.Object); for (int i = 0; i < 5; i++) { await service.CreateAsync(new CreateMessageDTO { ReceiverId = $"Receiver {i}", SenderId = $"Sender {i}", Content = "test", }); } Assert.Equal(5, list.Count); }
public async Task CreateAsync_Returns_EntityId() { //Arrange Message createdMessageEntity = null; _messageRepositoryMock.Setup(repository => repository.CreateAsync(It.IsAny <Message>())) .ReturnsAsync(_messageEntity) .Callback <Message>(messageEntity => createdMessageEntity = messageEntity ); //Act var result = await _service.CreateAsync(_correctMessageModel); _messageRepositoryMock.Verify(repository => repository.CreateAsync(It.IsAny <Message>()), Times.Once); //Asserts Assert.Equal(_messageEntity.Id, createdMessageEntity.Id); Assert.Equal(_messageEntity.Body, createdMessageEntity.Body); Assert.Equal(_messageEntity.Type, createdMessageEntity.Type); Assert.Equal(_messageEntity.DeliveryStatus, createdMessageEntity.DeliveryStatus); }
public async Task CreateMethodShouldAddMessageInDatabase() { var guid = Guid.NewGuid().ToString(); var options = new DbContextOptionsBuilder <ForumDbContext>() .UseInMemoryDatabase(guid) .Options; var db = new ForumDbContext(options); var dateTimeProviderMock = new Mock <IDateTimeProvider>(); var messagesService = new MessagesService(db, null, dateTimeProviderMock.Object); await messagesService.CreateAsync("Test", "123", "321"); db.Messages.Should().HaveCount(1); }
public async Task CreateAsync_ShouldSuccessfullyCreate() { // Arrange var context = ApplicationDbContextInMemoryFactory.InitializeContext(); var messageRepository = new EfDeletableEntityRepository <Message>(context); var messagesService = new MessagesService(messageRepository); // Act var expectedMessagesCount = 1; await messagesService.CreateAsync("testSenderId", "testReceiverId", "testContent"); var actualMEssagesCount = messageRepository.All().Count(); // Assert Assert.Equal(expectedMessagesCount, actualMEssagesCount); }
public async Task DeleteByIdAsync_WithCorrectData_ShouldSuccessfullyDelete() { var testContent = "TestContent"; // Arrange var context = ApplicationDbContextInMemoryFactory.InitializeContext(); var messageRepository = new EfDeletableEntityRepository <Message>(context); var messagesService = new MessagesService(messageRepository); await messagesService.CreateAsync("testSenderId", "testReceiverId", testContent); var comment = messageRepository.All().FirstOrDefault(c => c.Content == testContent); // Act var expectedCommentsCount = 0; await messagesService.DeleteByIdAsync(comment.Id); var actualCommentsCount = messageRepository.All().Count(); // Assert Assert.Equal(expectedCommentsCount, actualCommentsCount); }
public async Task MessageContentShouldBeCorrect() { var list = new List <Message>(); var mockRepository = new Mock <IDeletableEntityRepository <Message> >(); mockRepository .Setup(x => x.All()) .Returns(list.AsQueryable()); mockRepository .Setup(x => x.AddAsync(It.IsAny <Message>())) .Callback((Message message) => list.Add(message)); var service = new MessagesService(mockRepository.Object); await service.CreateAsync(new CreateMessageDTO { ReceiverId = "a", SenderId = "b", Content = "test", }); Assert.Equal("test", list.FirstOrDefault(x => x.ReceiverId == "a" && x.SenderId == "b")?.Content); }