public async void ShouldThrowValidationExceptionOnCreateWhenCreatedDateIsNotRecentAndLogItAsync( int minutes) { // given DateTimeOffset randomDate = GetRandomDateTime(); Contact randomContact = CreateRandomContact(randomDate); Contact invalidContact = randomContact; invalidContact.CreatedDate = randomDate.AddMinutes(minutes); invalidContact.UpdatedDate = invalidContact.CreatedDate; var invalidContactException = new InvalidContactException(); invalidContactException.AddData( key: nameof(Contact.CreatedDate), values: $"Date is not recent"); var expectedContactValidationException = new ContactValidationException(invalidContactException); this.dateTimeBrokerMock.Setup(broker => broker.GetCurrentDateTime()) .Returns(randomDate); // when ValueTask <Contact> createContactTask = this.contactService.AddContactAsync(invalidContact); // then await Assert.ThrowsAsync <ContactValidationException>(() => createContactTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameValidationExceptionAs( expectedContactValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertContactAsync(It.IsAny <Contact>()), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }
public async void ShouldThrowValidationExceptionOnCreateIfContactIsInvalidAndLogItAsync( string invalidText) { // given var invalidContact = new Contact { Information = invalidText, Notes = invalidText }; var invalidContactException = new InvalidContactException(); invalidContactException.AddData( key: nameof(Contact.Id), values: "Id is required"); invalidContactException.AddData( key: nameof(Contact.Information), values: "Text is required"); invalidContactException.AddData( key: nameof(Contact.Notes), values: "Text is required"); invalidContactException.AddData( key: nameof(Contact.CreatedDate), values: "Date is required"); invalidContactException.AddData( key: nameof(Contact.UpdatedDate), values: "Date is required"); invalidContactException.AddData( key: nameof(Contact.CreatedBy), values: "Id is required"); invalidContactException.AddData( key: nameof(Contact.UpdatedBy), values: "Id is required"); var expectedContactValidationException = new ContactValidationException(invalidContactException); // when ValueTask <Contact> createContactTask = this.contactService.AddContactAsync(invalidContact); // then await Assert.ThrowsAsync <ContactValidationException>(() => createContactTask.AsTask()); this.dateTimeBrokerMock.Verify(broker => broker.GetCurrentDateTime(), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameValidationExceptionAs( expectedContactValidationException))), Times.Once); this.storageBrokerMock.Verify(broker => broker.InsertContactAsync(It.IsAny <Contact>()), Times.Never); this.dateTimeBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); this.storageBrokerMock.VerifyNoOtherCalls(); }