public async Task ShouldThrowValidationExceptionOnRegisterIfStudentAlreadyExistsAndLogItAsync() { // given Student randomStudent = CreateRandomStudent(); Student inputStudent = randomStudent; Student storageStudent = inputStudent; string someMessage = new MnemonicString().GetValue(); var duplicateKeyException = new DuplicateKeyException(someMessage); var alreadyExistsStudentException = new AlreadyExistsStudentException(duplicateKeyException); var expectedStudentValidationException = new StudentValidationException(alreadyExistsStudentException); this.storageBrokerMock.Setup(broker => broker.InsertStudentAsync(inputStudent)) .ThrowsAsync(duplicateKeyException); // when ValueTask <Student> submitStudentTask = this.studentService.RegisterStudentAsync(inputStudent); // then await Assert.ThrowsAsync <StudentValidationException>(() => submitStudentTask.AsTask()); this.storageBrokerMock.Verify(broker => broker.InsertStudentAsync(inputStudent), Times.Once); this.loggingBrokerMock.Verify(broker => broker.LogError(It.Is(SameExceptionAs(expectedStudentValidationException))), Times.Once); this.storageBrokerMock.VerifyNoOtherCalls(); this.loggingBrokerMock.VerifyNoOtherCalls(); }
public void ShouldRenderStudentName() { // given ComponentState expectedState = ComponentState.Content; string randomStudentName = new MnemonicString().GetValue(); string returnedStudentName = randomStudentName; string expectedStudentName = returnedStudentName; this.studentServiceMock.Setup(service => service.GetStudentName()) .Returns(returnedStudentName); // when this.renderedNameComponent = RenderComponent <NameComponent>(); // then this.renderedNameComponent.Instance.StudentName .Should().BeEquivalentTo(expectedStudentName); this.renderedNameComponent.Instance.TextBox.GetValue() .Should().BeEquivalentTo(expectedStudentName); this.renderedNameComponent.Instance.State .Should().BeEquivalentTo(expectedState); this.renderedNameComponent.Instance.Exception .Should().BeNull(); this.studentServiceMock.Verify(service => service.GetStudentName(), Times.Once); this.studentServiceMock.VerifyNoOtherCalls(); }
public static T FillObject <T>() where T : class { var randomStringGenerator = new MnemonicString(2); var filler = new Filler <T>(); filler.Setup().OnType <ByteString>().Use(ByteString.CopyFrom(randomStringGenerator.GetValue(), Encoding.Unicode)); filler.Setup().OnType <Timestamp>().Use(Timestamp.FromDateTime(DateTime.UtcNow)); //should be UTC filler.Setup().OnType <Duration>().Use(Duration.FromTimeSpan(new TimeSpan(100))); return(filler.Create()); }
public void ShouldThrowForeignKeyConstraintConflictException() { // given int sqlForeignKeyConstraintConflictErrorCode = 547; string randomErrorMessage = new MnemonicString().GetValue(); SqlException foreignKeyConstraintConflictException = CreateSqlException(); var dbUpdateException = new DbUpdateException( message: randomErrorMessage, innerException: foreignKeyConstraintConflictException); this.sqlErrorBrokerMock.Setup(broker => broker.GetSqlErrorCode(foreignKeyConstraintConflictException)) .Returns(sqlForeignKeyConstraintConflictErrorCode); // when . then Assert.Throws <ForeignKeyConstraintConflictException>(() => this.efxceptionService.ThrowMeaningfulException(dbUpdateException)); }
public void ShouldThrowInvalidObjectNameException() { // given int sqlInvalidObjectNameErrorCode = 208; string randomErrorMessage = new MnemonicString().GetValue(); SqlException invalidObjectNameException = CreateSqlException(); var dbUpdateException = new DbUpdateException( message: randomErrorMessage, innerException: invalidObjectNameException); this.sqlErrorBrokerMock.Setup(broker => broker.GetSqlErrorCode(invalidObjectNameException)) .Returns(sqlInvalidObjectNameErrorCode); // when . then Assert.Throws <InvalidObjectNameException>(() => this.efxceptionService.ThrowMeaningfulException(dbUpdateException)); }
public void ShouldThrowDuplicateKeyWithUniqueIndexException() { // given int sqlDuplicateKeyErrorCode = 2601; string randomErrorMessage = new MnemonicString().GetValue(); SqlException duplicateKeySqlException = CreateSqlException(); var dbUpdateException = new DbUpdateException( message: randomErrorMessage, innerException: duplicateKeySqlException); this.sqlErrorBrokerMock.Setup(broker => broker.GetSqlErrorCode(duplicateKeySqlException)) .Returns(sqlDuplicateKeyErrorCode); // when . then Assert.Throws <DuplicateKeyWithUniqueIndexException>(() => this.efxceptionService.ThrowMeaningfulException(dbUpdateException)); }
/// <summary> /// Sets the default randomizer for all simple types. /// </summary> private void SetDefaultRandomizer() { var mnemonic = new MnemonicString(20); var doublePlugin = new DoubleRange(); var dateTimeRandomizer = new DateTimeRange(new DateTime(1970, 1, 1)); this.TypeToRandomFunc[typeof(string)] = mnemonic.GetValue; this.TypeToRandomFunc[typeof(bool)] = () => Random.Next(0, 2) == 1; this.TypeToRandomFunc[typeof(bool?)] = () => new RandomListItem<bool?>(true, false, null).GetValue(); this.TypeToRandomFunc[typeof(short)] = () => (short)Random.Next(-32767, 32767); this.TypeToRandomFunc[typeof(short?)] = () => (short)Random.Next(-32767, 32767); this.TypeToRandomFunc[typeof(int)] = () => Random.Next(); this.TypeToRandomFunc[typeof(int?)] = () => Random.Next(); this.TypeToRandomFunc[typeof(long)] = () => Random.NextLong(); this.TypeToRandomFunc[typeof(long?)] = () => Random.NextLong(); this.TypeToRandomFunc[typeof(float)] = () => (float)doublePlugin.GetValue(); this.TypeToRandomFunc[typeof(float?)] = () => (float?)doublePlugin.GetValue(); this.TypeToRandomFunc[typeof(double)] = () => doublePlugin.GetValue(); this.TypeToRandomFunc[typeof(double?)] = () => doublePlugin.GetValue(); this.TypeToRandomFunc[typeof(decimal)] = () => (decimal)Random.Next(); this.TypeToRandomFunc[typeof(decimal?)] = () => (decimal)Random.Next(); this.TypeToRandomFunc[typeof(Guid)] = () => Guid.NewGuid(); this.TypeToRandomFunc[typeof(Guid?)] = () => Guid.NewGuid(); this.TypeToRandomFunc[typeof(DateTime)] = () => dateTimeRandomizer.GetValue(); this.TypeToRandomFunc[typeof(DateTime?)] = () => dateTimeRandomizer.GetValue(); this.TypeToRandomFunc[typeof(byte)] = () => (byte)Random.Next(); this.TypeToRandomFunc[typeof(byte?)] = () => (byte?)Random.Next(); this.TypeToRandomFunc[typeof(char)] = () => (char)Random.Next(); this.TypeToRandomFunc[typeof(char?)] = () => (char)Random.Next(); this.TypeToRandomFunc[typeof(ushort)] = () => (ushort)Random.Next(); this.TypeToRandomFunc[typeof(ushort?)] = () => (ushort)Random.Next(); this.TypeToRandomFunc[typeof(uint)] = () => (uint)Random.Next(); this.TypeToRandomFunc[typeof(uint?)] = () => (uint)Random.Next(); this.TypeToRandomFunc[typeof(ulong)] = () => (ulong)Random.Next(); this.TypeToRandomFunc[typeof(ulong?)] = () => (ulong)Random.Next(); this.TypeToRandomFunc[typeof(IntPtr)] = () => default(IntPtr); this.TypeToRandomFunc[typeof(IntPtr?)] = () => default(IntPtr); this.TypeToRandomFunc[typeof(TimeSpan)] = () => new TimeSpan(Random.Next()); this.TypeToRandomFunc[typeof(TimeSpan?)] = () => new TimeSpan(Random.Next()); }