Exemple #1
0
 public static void ThrowIfDuplicateKey(this MongoWriteException ex, string fieldKey, string errorMessage)
 {
     if (ex.WriteError != null && ex.WriteError.Category == ServerErrorCategory.DuplicateKey && ex.WriteError.Code == 11000)
     {
         var alreadyExistsException = new AlreadyExistsException();
         alreadyExistsException.AddModelError(fieldKey, errorMessage);
         throw alreadyExistsException;
     }
 }
        public void WhenCallEdit_AndTheCommandIsSave_AndThrowAlreadyExistsException_TheShouldReturnEditViewWithModelStateInvalid()
        {
            // Arrange
            var clientInputModel       = CreateClientModel("1", "secret").ToInputModel();
            var alreadyExistsException = new AlreadyExistsException();

            alreadyExistsException.AddModelError("someKey", "someMessage");

            ClientServiceMock.Setup(service => service.UpsertClientAsync(It.IsAny <ClientInputModel>())).Throws(alreadyExistsException);

            // Act
            var actionResult      = ClientController.Edit(clientInputModel, ControllerConstants.SAVE, string.Empty).GetAwaiter().GetResult() as ViewResult;
            var actionResultModel = actionResult?.Model as ClientInputModel;

            // Assert
            Assert.IsFalse(ClientController.ModelState.IsValid);
            Assert.IsNotNull(actionResult);
            Assert.AreEqual(clientInputModel, actionResultModel);
        }