public void Save_should_throw_exception() { var validationService = new Mock <IValidationService>(); validationService.Setup(x => x.Validate(It.IsAny <CharacterEditModel>())).Returns(new List <ValidationError>()); var characterDao = new Mock <ICharacterDao>(); characterDao.Setup(x => x.Load(It.IsAny <Guid>())).Returns(null as Character); ICharacterOperationService operationService = new CharacterOperationService(validationService.Object, characterDao.Object); Assert.Throws <InvalidOperationException>(() => operationService.Save(new CharacterEditModel { Id = Guid.NewGuid() })); }
public void Save_should_return_validation_errors() { var error = new ValidationError("Property", "Message"); var validationService = new Mock <IValidationService>(); validationService.Setup(x => x.Validate(It.IsAny <CharacterEditModel>())).Returns(new List <ValidationError>() { error }); ICharacterOperationService operationService = new CharacterOperationService(validationService.Object, null); var result = operationService.Save(new CharacterEditModel()); Assert.IsFalse(result.IsValid); Assert.AreEqual(1, result.Errors.Count); Assert.AreSame(error, result.Errors.First()); }
public void Save_should_create_new_character() { var validationService = new Mock <IValidationService>(); validationService.Setup(x => x.Validate(It.IsAny <CharacterEditModel>())).Returns(new List <ValidationError>()); var characterDao = new Mock <ICharacterDao>(); ICharacterOperationService operationService = new CharacterOperationService(validationService.Object, characterDao.Object); var model = new CharacterEditModel { Name = "Zeratul" }; var result = operationService.Save(model); Assert.IsTrue(result.IsValid); characterDao.Verify(x => x.Load(It.IsAny <Guid>()), Times.Never); characterDao.Verify(x => x.Save(It.Is <Character>(c => ReferenceEquals(c.Name, model.Name))), Times.Once); }