当前位置 > 首页 > Asp.net

Writing Unit Tests for an ASP.NET MVC Action Method that handles Ajax Request and Normal Request

2012-12-9 16:13:00来源:Asp.net

In this blog post, I will demonstrate how to write unit tests for an ASP.NET MVC action method, which handles both Ajax request and normal HTTP Request. I will write a unit test for specifying the behavior of an Ajax request and will write another unit test for specifying the behavior of a normal HTTP request. Both Ajax request and normal request will be handled by a single action method. So the ASP.NET MVC action method will be execute HTTP Request object’s IsAjaxRequest method for identifying whether it is an Ajax request or not. So we have to create mock object for Request object and also have to make as a Ajax request from the unit test for verifying the behavior of an Ajax request. I have used NUnit and Moq for writing unit tests.

Let me write a unit test for a Ajax request

Code Snippet
  1. [Test]
  2. public void Index_AjaxRequest_Returns_Partial_With_Expense_List()
  3. {
  4. ????// Arrange??
  5. ????Mock<HttpRequestBase> request = new Mock<HttpRequestBase>();
  6. ????Mock<HttpResponseBase> response = new Mock<HttpResponseBase>();
  7. ????Mock<HttpContextBase> context = new Mock<HttpContextBase>();
  8.  
  9. ????context.Setup(c => c.Request).Returns(request.Object);
  10. ????context.Setup(c => c.Response).Returns(response.Object);
  11. ????//Add XMLHttpRequest request header
  12. ????request.Setup(req => req["X-Requested-With"]).
  13. ????????Returns("XMLHttpRequest");
  14.  
  15. ????IEnumerable<Expense> fakeExpenses = GetMockExpenses();
  16. ????expenseRepository.Setup(x => x.GetMany(It.
  17. ????????IsAny<Expression<Func<Expense, bool>>>())).
  18. ????????Returns(fakeExpenses);
  19. ????ExpenseController controller = new

  • 工作流引擎设计中关于处理时限的遐想

    2009-2-5 1:12:00

    摘要: 最近总有网友来咨询我和我的同事关于工作流引擎设计方面的一些思路,有一个很明显的感触,很多朋友在设计工作流引擎之前 已经习惯了一种“契约式”编程设计的方式,也就是说在明

  • mmseg4j 中文分词 for .net版本

    2011-7-19 16:12:00

    1、mmseg4j 用 Chih-Hao Tsai 的 MMSeg 算法( http://technology.chtsai.org/mmseg/ )实现的中文分词器,并实现 lucene 的

  • Creating Basic RSS Reader in ASP.NET MVC 3

    2011-8-17 4:53:00

    In this post I am going to explain you how we can create a basic RSS Reader with the help of Linq-T

  • 论欧喷索斯应当缓行

    2007-6-27 19:52:00

    摘要: 鲁迅曾经写过一篇“论费耳颇赖应当缓行”的文章,“费耳颇赖”是英文“fare play”的音译,意思是“公平竞争”。鲁迅并没有发对“费耳颇赖”,只是说在当时的国情,“费耳颇赖”是不可行的,是不

  • OAuth in action – Linq2Twitter

    2009-8-20 0:13:00

    The other day I came across a pretty cool project, Linq2Twitter , that basically implements a linq

  • 我就是那个福娃

    2007-5-10 11:13:00

    很小以来,对于大型充气玩具一直充满好奇,也曾经在麦当劳门口看到麦当劳叔叔的充气娃娃,甚至过去和他握手,询问他在里面感觉如何。而在刚过去的五一节当中,我也体验了一把做充气娃娃的感觉。(本来想让大家猜猜

  • .net 最佳实践一:监测.net代码中的高内存消耗函数

    2009-8-30 0:17:00

    摘要: Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 .net 最佳实践一:监测.net代码中的高内存消耗函数 简

  • Visual Basic 9 不完全入门系列(1) : 概览

    2007-5-28 19:18:00

    摘要: Visual Studio Codename Orcas Beta 1 的发布,Visual Basic 9 这只“昔日王谢堂前燕”也终于“飞入寻常百姓家”了。 阅读全文 随风流月

  • WF4设计器模型:编辑范围ModelEditingScope

    2009-11-27 23:43:00

    阅读: 26 评论: 0 作者: 生鱼片 发表于 2009-11-27 22:55 原文链接 由于WF4的设计器用WPF来呈现,所以本文的理论也适用于WPF。当你在WF4的设计器中选中某些

  • [你必须知道的.NET]第三十四回,object成员,不见了!

    2009-10-30 2:45:00

    阅读: 72 评论: 9 作者: Anytao 发表于 2009-10-30 00:47 原文链接 anytao.net | 《你必须知道的.NET》网站 | A