Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 14th, 2011, 02:18 PM
Friend of Wrox
 
Join Date: Mar 2010
Posts: 99
Thanks: 21
Thanked 6 Times in 4 Posts
Default Why is this NUnit test pass and not fail ?

Hi,

I spend over 2 days on this... i can't figure out why this test is passing.

I mean.. it shouldn't return the view "Completed" but in the test it does!

It say Expected "string.Empty" but returned "Completed" however for the creditcard to pass.. the securitycode need to be "test" which is isnt at all in the test. So it should return the default view (which is like view();).

Here the exact error :

SportsStore.UnitTests.ShoppingCart.Cannot_Check_Ou t_If_Credit_Card_Failed_To_Process:
Expected string length 0 but was 9. Strings differ at index 0.
Expected: <string.Empty>
But was: "Completed"
-----------^

What i am doing wrong ? it is my test that is wrong ? or the controller logic ?

Thanks a lots.



***

[Test]
public void Cannot_Check_Out_If_Credit_Card_Failed_To_Process( )
{
var mockOrderSubmitter = new Mock<IOrderSubmitter>();
var mockCreditCardProcessor = new Mock<ICreditCardProcessor>();

// Arrange: Given a user has a non-empty cart
var cart = new Cart();
cart.AddItem(new Product(), 1);

// Arrange: ... but the credit card failed to process
var cartController = new CartController(null, mockOrderSubmitter.Object, mockCreditCardProcessor.Object);
var result = cartController.CheckOut(cart, new ShippingDetails(), new CreditCard() { SecurityCode = "123" });

// Assert
result.ShouldBeDefaultView();
}

***

[HttpPost]
public ActionResult CheckOut(Cart cart, ShippingDetails shippingDetails, CreditCard creditCard)
{
// Empty carts can't be checked out
if (cart.Lines.Count == 0)
ModelState.AddModelError("Cart", "Sorry, your cart is empty!");

// Everything is valid
if (ModelState.IsValid)
{
// Effectue le paiement.
TransactionResult result = creditcardProcessor.TakePayment(creditCard, cart.ComputeTotalValue());
if (result == TransactionResult.Success)
{
// Envoi la commande
orderSubmitter.SubmitOrder(cart, shippingDetails);
cart.Clear();
return View("Completed");
}
else
{
ModelState.AddModelError("CreditCard", "Sorry, we couldn't process your credit card, please verify your credit card details and retry.");
return View(new CheckOutViewModel());
}

}
else // Something was invalid
return View(new CheckOutViewModel());
}

***

public class MainCreditCardProcessor : ICreditCardProcessor
{
public TransactionResult TakePayment(CreditCard card, decimal amount)
{
if (card.SecurityCode == "test")
return TransactionResult.Success;
else
return TransactionResult.TransactionDeclined;
}
}

***
 
Old January 16th, 2011, 10:39 PM
Friend of Wrox
 
Join Date: Mar 2010
Posts: 99
Thanks: 21
Thanked 6 Times in 4 Posts
Default

I found a solution after 1 week of searching.

The problem was with my test. Since mocks objects are empty objects with no methods inside, zero logics. That mean that the test actually doesn't use an instance of the MainCreditCarDProcessor.

So i had to setup the mock for this particuliar test by adding :

// Arrange: Given we have a creditcard processor that return TransactionDeclined.
mockCreditCardProcessor.SetReturnsDefault(Transact ionResult.TransactionDeclined);





Similar Threads
Thread Thread Starter Forum Replies Last Post
Installing NUnit extensions for MVC 2 with web developer express ? Rushino BOOK: Professional ASP.NET MVC 2 1 December 31st, 2010 11:29 PM
Pass value to second page (create & pass var) ismailc ASP.NET 2.0 Basics 8 April 24th, 2010 07:03 AM
Httpcontext.Current doesn't work with NUnit myself.panku BOOK: ASP.NET MVC Website Programming Problem Design Solution ISBN: 9780470410950 0 January 20th, 2010 08:53 AM
Fail over Replication carumuga SQL Server 2005 1 September 25th, 2008 06:34 PM
Why DB_CREATE_FILE_DEST is fail? Edward King Oracle 1 May 2nd, 2005 03:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.