public void AddMedicine_WhenCalled_WithValidData_ShouldReturnProperResponse() { var medicineDto = new MedicineDto() { FullName = "Medicine-1", Price = 1.0M, ExpiryDate = DateTime.Now.AddDays(-1) }; _serviceMock.Setup(_ => _.AddMedicine(It.IsAny <MedicineDto>())).Returns(() => { throw new ValidationException("Invalid data"); }); Assert.Throws <ValidationException>(() => _controller.AddMedicine(medicineDto)); _serviceMock.Verify(_ => _.AddMedicine(It.IsAny <MedicineDto>()), Times.Once); }
public void AddMedicine_CorrectData_AddedNewMedicine() { // Arrange var model = new MedicineInputModel { Name = "Nowy lek", ContentQuantity = "100ml", Price = 23.49M, Description = "Nowy jeszcze lepszy super lek", Producer = "Pharma", MedicineTypeId = 1 }; mock.Setup(x => x.CreateNewMedicine(model)).Returns(() => new OperationResult <string>(ResultResources.CreatingMedicineComplete)); var medicinesController = new MedicinesController(mock.Object); // Act var result = medicinesController.AddMedicine(model) as OkObjectResult; var response = result.Value as Result <string>; // Assert Assert.AreEqual((int)HttpStatusCode.OK, result.StatusCode); Assert.IsNotNull(response.Items); Assert.AreEqual(ResultResources.CreatingMedicineComplete, response.Items); }
public void AddMedicineTest() { medicineViewModel.MedicineName = "Panadol"; medicineViewModel.Description = "For Headache"; medicineViewModel.Expiry = "06/06/2015"; medicineViewModel.ShelfLife = 30; medicineViewModel.Value = 25.00; medicineViewModel.IstempSensitve = false; medicineController.AddMedicine(medicineViewModel); }
public void AddMedicine_NullInputModel_ModelStateError() { // Arrange var medicinesController = new MedicinesController(It.IsAny <IMedicinesRepository>()); // Act var result = medicinesController.AddMedicine(It.IsAny <MedicineInputModel>()) as BadRequestObjectResult; var response = result.Value as List <OperationError>; // Assert Assert.AreEqual((int)HttpStatusCode.BadRequest, result.StatusCode); Assert.IsNotNull(response); Assert.AreEqual(ErrorResources.ModelStateError, response.Single().Message); }
private void btnAdd_Click(object sender, RoutedEventArgs e) { try { MyDbContext context = new MyDbContext(); MedicinesController medicinesController = new MedicinesController(); if (double.TryParse(PriceMedicine.Text, out double price) == true && int.TryParse(AmmountMedicine.Text, out int ammount) == true && int.TryParse(NameMedicine.Text, out int medicineint) == false && int.TryParse(CategoriesMedicine.Text, out int categoryint) == false && int.TryParse(ProviderMedicine.Text, out int providerint) == false ) { double priceMed = double.Parse(PriceMedicine.Text); int ammountMed = int.Parse(AmmountMedicine.Text); Medicine medicine = new Medicine(NameMedicine.Text, CategoriesMedicine.Text, ProviderMedicine.Text, priceMed, ammountMed); if (medicinesController.AddMedicine(medicine)) { MessageBox.Show("Препарат успешно добавлен."); MedicineDataGrid.ClearValue(ItemsControl.ItemsSourceProperty); List <Medicine> medicines = context.Medicines.Include(med => med.Provider).Include(med1 => med1.Categories).ToList(); MedicineDataGrid.ItemsSource = medicines; } else { MessageBox.Show("Этот препарат уже существует или введенные данные имеют неверный формат."); } } else { MessageBox.Show("Неверный формат данных"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public void AddMedicine_InvalidData_ManyValidationErrors() { // Arrange var model = new MedicineInputModel { Name = "", ContentQuantity = "1", Price = 23.49M, Description = " ", Producer = " P ", MedicineTypeId = 0 }; mock.Setup(x => x.CreateNewMedicine(model)) .Returns(() => new OperationResult <string>(new List <OperationError> { new OperationError((int)ErrorCodes.BadRequest, ErrorResources.FieldCannotBeEmpty, nameof(model.Name)), new OperationError((int)ErrorCodes.BadRequest, string.Format(ErrorResources.InvalidFieldLength, 2, 10), nameof(model.ContentQuantity)), new OperationError((int)ErrorCodes.BadRequest, ErrorResources.FieldCannotBeEmpty, nameof(model.Description)) })); var medicinesController = new MedicinesController(mock.Object); // Act var result = medicinesController.AddMedicine(model) as BadRequestObjectResult; var response = result.Value as IEnumerable <OperationError>; // Assert Assert.AreEqual((int)HttpStatusCode.BadRequest, result.StatusCode); Assert.IsNotNull(response); Assert.AreEqual(3, response.Count()); Assert.AreEqual((int)ErrorCodes.BadRequest, response.First().ErrorCode); }