public void Creating_A_Valid_Supplier_Returns_Created() { var mockUrlHelper = new Mock <IUrlHelper>(MockBehavior.Strict); Expression <Func <IUrlHelper, string> > urlSetup = url => url.Action(It.Is <UrlActionContext>(uac => uac.Action == "Get")); mockUrlHelper.Setup(urlSetup).Returns("a/mock/url/for/testing").Verifiable(); _controller.Url = mockUrlHelper.Object; _supplierService.Setup(ps => ps.Add(It.IsAny <Supplier>())).Returns(1); _supplierService.Setup(ss => ss.Validate(It.IsAny <Supplier>())).Returns(new ValidationResult() { }); var result = _controller.Add(new Supplier()); Assert.That(result, Is.TypeOf <CreatedResult>()); _supplierService.Verify(ss => ss.Add(It.IsAny <Supplier>()), Times.Once); }
private void dataGridViewSuppliers_CellContentClick(object sender, DataGridViewCellEventArgs e) { try { if (e.RowIndex == -1) //редактрование с второй строки { return; } int taskIndex = dataGridViewSuppliers.Rows[e.RowIndex].Cells["Операция"].ColumnIndex; if (e.ColumnIndex == taskIndex) { string task = dataGridViewSuppliers.Rows[e.RowIndex].Cells["Операция"].Value.ToString(); if (task == "Удалить") { if (MessageBox.Show("Удалить эту строку?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { int id = (int)dataGridViewSuppliers.CurrentRow.Cells["ID"].Value; supplierController.Delete(id); } } else if (task == "Добавить") { int rowIndex = dataGridViewSuppliers.Rows.Count - 2; DataGridViewRow currentRow = dataGridViewSuppliers.Rows[rowIndex]; Supplier newSupplier = GetSupplierInfo(ref currentRow); if (newSupplier == null) { return; } if (newSupplier.Phone.Length > 11) { MessageBox.Show("Номер телефона должен состоять не более чем из 11 цифр."); return; } int currentSupplierId = supplierController.GetSupplierIdByPhone(newSupplier.Phone); if (currentSupplierId != 0) { MessageBox.Show("Поставщик с введенным номером телефона уже существует."); return; } supplierController.Add(newSupplier); dataGridViewSuppliers.Rows[e.RowIndex].Cells["Операция"].Value = "Удалить"; } else if (task == "Изм.") { int rowIndex = e.RowIndex; DataGridViewRow currentRow = dataGridViewSuppliers.Rows[rowIndex]; Supplier updatedSupplier = GetSupplierInfo(ref currentRow); if (updatedSupplier == null) { return; } int currentSupplierId = supplierController.GetSupplierIdByPhone(updatedSupplier.Phone); if (updatedSupplier.ID != currentSupplierId && currentSupplierId != 0) { MessageBox.Show("Поставщик с введенным номером телефона уже существует."); return; } supplierController.Edit(updatedSupplier); currentRow.Cells["Операция"].Value = "Удалить"; } supplierController.GetAllSuppliers(ref dataGridViewSuppliers); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error); } }