Reputation: 11
I'm working my way through the book, Pro ASP.Net MVC3 Framework by Adam Freeman and Stephen Sanderson (Apress). Near the end of chapter 8 we have created a shopping cart application and unit tests. I have had no problems to this point, but now a unit test is failing for a reason I don't understand. Errata for the book has no mention of this problem.
I am using Windows 7 64-bit, .Net 4, VS 2010 Ultimate.
An instance of a ShippingDetails class (shipping address, etc.) and an instance of a Cart class (items ordered) are passed to the CartController's "Checkout" action, which returns a ViewResult. If ModelState is valid, processing is done, the cart is cleared and View("Completed") is returned. If ModelState is not valid, View(shippingDetails) is returned (shippingDetails being the passed-in parameter).
We created a unit test to insure that an empty cart cannot be checked out. A CartController is instantiated and its Checkout action called, passing new instances of Cart and ShippingDetails, so that the count of items in the cart is 0.
On return from the controller's Checkout action (in unit test debug mode) I am able to hover over result.ViewData in subsequent code ("result" being the returned ViewResult) and see "result.ViewData threw an exception of type 'System.ArgumentException'". Expanding the information, I see for "base" and "Message", "Cannot find the method on the object instance." The failed unit test's details say: Test method SportsStore.UnitTests.CartTests.Cannot_Checkout_Empty_Cart threw exception: System.EntryPointNotFoundException: Entry point was not found.
Here is the CartController "Checkout" action:
[HttpPost]
public ViewResult Checkout(Cart cart, ShippingDetails shippingDetails)
{
if (cart.Lines.Count() == 0) {
ModelState.AddModelError("Cart", "Sorry, your cart is empty!");
}
if (ModelState.IsValid) {
orderProcessor.ProcessOrder(cart, shippingDetails);
cart.Clear();
return View("Completed");
} else {
return View(shippingDetails);
}
}
Here is the unit test:
[TestMethod]
public void Cannot_Checkout_Empty_Cart()
{
// Arrange
Mock<IOrderProcessor> mock = new Mock<IOrderProcessor>();
Cart cart = new Cart();
ShippingDetails shippingDetails = new ShippingDetails();
CartController target = new CartController(null, mock.Object);
// Act
ViewResult result = target.Checkout(cart, shippingDetails);
// Assert - check that the order hasn't been passed on to the processor
mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()),
Times.Never());
// Assert - check that the method is returning the default view
Assert.AreEqual("", result.ViewName);
// Assert - check that we are passing an invalid model to the view
Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
}
Your help is appreciated. Note that this was previously posted on the ASP.Net MVC forum, but got no response.
Upvotes: 1
Views: 1343
Reputation: 980
I was able to solve this issue, it seems my test project was referencing MVC 4 while the web project was referencing MVC 3. I made the test project to reference MVC 3 and now its working just fine
Upvotes: 1