0
我正在嘗試測試我的控制器,並且對於此測試頗爲新穎。使用NUnit和Moq我一直得到一個空結果。這是我的測試,我錯過了一個步驟?就像我提到這是我的第一個項目。MVC控制器單元測試
[TestFixture]
class CustomerServiceTests
{
public Mock<IRepository<Customer>> CustomerRepository = new Mock<IRepository<Customer>>();
public Customer Customer;
[SetUp]
public void Setup()
{
Customer = new Customer()
{
Id = 1 << Can I set the ID?
Address = "3 Lakeview Terrace",
City = "New York",
Email = "[email protected]",
FirstName = "Joe",
LastName = "Dirt",
Phone = "888-888-8888",
Province = "NY"
};
}
[Test]
public void CanCreateCustomer()
{
// ARRANGE
var controller = new CustomerController(CustomerRepository.Object);
controller.Create(Customer);
// ACT
var customer = CustomerRepository.Setup(c => c.Find(1)).Returns(new Customer());
// ASSERT
Assert.AreEqual(Customer, customer);
}
}
控制器
// POST: /Customer/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Customer customer)
{
if (ModelState.IsValid)
{
_customerRepository.Add(customer);
return RedirectToAction("Index");
}
return View(customer);
}
IREPOSITORY
public interface IRepository<T> where T : class
{
IQueryable<T> Get { get; }
T Find(object[] keyValues);
T Find(int id);
void Add(T entity);
void Update(T entity);
void AddOrUpdate(T entity);
void Remove(object[] keyValues);
void Remove(T entity);
}
謝謝!只需要弄清楚你如何聲明視圖。 :) – devfunkd
你想測試什麼?視圖中的內容或您返回的視圖? – Rikard
最終,但現在只是什麼觀點正在返回。 – devfunkd