在我们的基于Music Store Tutorial的Entity Framework 4.0的MVC4应用程序中,我们使用Moq来模拟DbContext
,单元测试是逻辑。我们的一种方法很难测试,因为它使用了HttpContext
或HttpContextBase
。一个示例方法是这样的:如何在ShoppingCart控制器/模型中模拟HttpContext
public static ShoppingCart GetCart(HttpContextBase context)
{
var cart = new ShoppingCart();
cart.ShoppingCartId = cart.GetCartId(context);
return cart;
}
从HttpContextBase
收集的唯一属性是[CartSessionKey]
因为可以在这里看到:
public string GetCartId(HttpContextBase context)
{
if (context.Session[CartSessionKey] == null)
{
if (!string.IsNullOrWhiteSpace(context.User.Identity.Name))
{
context.Session[CartSessionKey] =
context.User.Identity.Name;
}
else
{
// Generate a new random GUID using System.Guid class
Guid tempCartId = Guid.NewGuid();
// Send tempCartId back to client as a cookie
context.Session[CartSessionKey] = tempCartId.ToString();
}
}
return context.Session[CartSessionKey].ToString();
}
我们听说的恐怖故事HttpContext
是一个非常复杂的类和如果你打印它,你有足够的纸张八次环绕地球。
不过我们想嘲笑它。问题是如何。我们想要模拟的属性是[CartSessionKey]
,以及来自上下文的属性为contest.User.Identity.Name
。
我们怀疑,我们需要使用这样的事情:
var mockData = new Mock<FakeContext>();
mockData.Setup(m => m.Orders).Returns(memoryOrderItems);
mockData.Setup(m => m.Carts).Returns(memoryCartItems);
Mock<HttpContextBase> mockHttpContext = new Mock<HttpContextBase>();
Mock<HttpRequestBase> mockHttpRequest = new Mock<HttpRequestBase>();
mockHttpRequest.Setup(x => x.CartSessionKey).Returns(1);
mockHttpContext.Setup(x => x.Request).Returns(mockHttpRequest.Object);
,但我们无法找到如何具体实现这个,所以我们没有得到在使用context.Session[CartSessionKey]
或context.User.Identity.Name
方法的任何错误。
我们希望有人能帮助我们。
/编辑
当我们这样做:
var memoryUserItems = new FakeDbSet<User>()
{
new User { Email = "[email protected]",
FullName = "Test Person",
isAvailable = true,
Name = "WHat"
},
new User { Email = "[email protected]",
FullName = "Test Person 2",
isAvailable = true,
Name = "WHat 2"
}
};
(...) Other memory...Items
然后将此:
// Create mock units of work
var mockData = new Mock<FakeContext>();
mockData.Setup(m => m.Orders).Returns(memoryOrderItems);
mockData.Setup(m => m.Carts).Returns(memoryCartItems);
mockData.Setup(m => m.Users).Returns(memoryUserItems);
var principalMock = new Mock<IPrincipal>();
var identityMock = new Mock<IIdentity>();
var userMock =
identityMock.Setup(x => x.Name).Returns("Test!");
identityMock.Setup(x => x.IsAuthenticated).Returns(true); // optional ;)
mockData.Setup(x => x.Identity).Returns(identityMock.Object);
var httpReqBase = new Mock<HttpRequestBase>(); // this is useful if you want to test Ajax request checks or cookies in the controller.
var httpContextBase = new Mock<HttpContextBase>();
httpContextBase.Setup(x => x.User).Returns(principalMock.Object);
httpContextBase.Setup(x => x.Session[It.IsAny<string>()]).Returns(1); //Here is the session indexer. You can swap 'any' string for specific string.
httpContextBase.Setup(x => x.Request).Returns(httpReqBase.Object);
我们得到的错误是:
错误3“项目。 Models.FakeContext'确实不是 包含“身份”和没有扩展方法 “身份”接受 类型“project.Models.FakeContext”的第一个参数的定义可以发现 (是否缺少使用指令或程序集 引用?)
/edit2
为了更清楚。我测试的实际方法如下:
public ActionResult Complete(int id)
{
// Make sure that user is currentuser and otherwise bring user to our Thief page
if (id != db.GetCurrentUserId())
{
return View("Thief");
}
var cart = ShoppingCart.GetCart(this.HttpContext);
var currentDate = DateTime.Today;
var viewModel = new ShoppingCartViewModel
{
CartItems = cart.GetCartItems(),
CartTotal = cart.GetTotal(),
ProductItems = db.Products.ToList()
};
if (viewModel.CartItems.Count() == 0)
{
return View("Empty");
}
// Try to write cart to order table
try
{
foreach (var item in viewModel.CartItems)
{
ProcessOrder(item, id, currentDate);
}
// after this we empty the shopping cart
cart.EmptyCart();
return View();
}
catch
{
// Invalid - display error page
return View("Error");
}
}
可以看出该var cart = ShoppingCart.GetCart(this.HttpContext);
使用this.HttpContext
。在测试中,我只是做controller.Complete(1)
。我猜想我无法将新的HttpContext
传递给控制器?
/编辑3
虽然使用下面的代码与嘲笑我得到以下信息:
Test Name: TestCheckoutCompleteShouldWithEmptyCart
Test FullName: Controllers.CheckoutControllerTest.TestCheckoutCompleteShouldWithEmptyCart
Test Source: Controllers\CheckoutControllerTest.cs : line 141
Test Outcome: Failed
Test Duration: 0:00:00.0158591
Result Message:
Test method Controllers.CheckoutControllerTest.TestCheckoutCompleteShouldWithEmptyCart threw exception:
System.NullReferenceException: Object reference not set to an instance of an object.
Result StackTrace:
at Models\ShoppingCart.cs:line 170
at \Models\ShoppingCart.cs:line 20
at \Controllers\CheckoutController.cs:line 48
at Controllers\CheckoutControllerTest.cs:line 143
您是否正在测试公共字符串GetCartId(HttpContextBase上下文)方法?你的MUT是什么? GetCart或GetCartId? – Spock
Hi @Spock Hi,什么是MUT? 'GetCartId'是GetCart依赖的主要关注点。 –
对不起,这是测试方法。你试图达到的目标非常简单。看到我的答案,还有其他几种做法。乐于帮助 – Spock