当前位置 > 首页 > Asp.net

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

12/9/2012 4:13:00 PM来源: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

  • 临时文件和页面程序集(Dino Esposito 著)

    5/14/2007 2:21:00 PM

    要全面了解 ASP.NET HTTP 运行时,让我们来分析一下当请求 ASP.NET 页面时,文件系统层所发生的变化。接下来,您将了解由 HTTP 管道的对象管理和监视的一组动态创建的临时文件。

  • 恢复被格式化过的硬盘数据

    7/24/2009 12:48:00 AM

    人一旦闲下来就会搞出一些事端来。项目刚刚结束,手头没有什么特别要紧的事情要做,所以打算重装一下系统。从公司局域网安装个 window 7 RC 版,等文件拷贝结束后感觉就死机了,后来自己重启,然后重

  • [叩响C#之门]写给初学者:多线程系列( 十)——死锁(Deadlock)

    9/9/2009 1:35:00 PM

    摘要: 前言:我第一次接触“线程”的概念时,觉得它深奥难懂,看了好多本书,花了很长时间才领悟到它的真谛。现在我就以一个初学者的心态,把我所理解的“多线程”描述给大家。这一次是系列文章,比较完整的展示

  • ASP.NET MVC 2 Beta 发布

    11/18/2009 9:11:00 PM

    阅读: 515 评论: 7 作者: 周利华 发表于 2009-11-18 17:55 原文链接 在软件业里从程序员出身的,千万不要把你自己的本行丢掉。我说的本行是技术。虽然在中国,死钻研技

  • Microsoft AJAX CDN – Now with SSL Support

    11/30/2009 1:27:00 PM

    Back in September I blogged about the new Microsoft AJAX CDN (Content Delivery Network) service

  • Weird trap while using WebForm_DoCallback() inside my external client scripts

    3/5/2007 9:57:00 PM

    Today, while I was trying to move large portions of a web application I'm working on from Ajax

  • ASP.NET MVC Razor IsPost property with IF –Else loop

    12/3/2010 7:59:00 PM

    ASP.NET MVC Razor a new view engine from Microsoft looks very promising. Here are example of code w

  • [WPF]实现密码框的密码绑定

    8/27/2009 2:47:00 PM

    摘要: 正如绑定TextBox控件的Text属性一样, 我们希望能够将PasswordBox空间的Password属性进行绑定, 比如在MVVM模式中,这似乎是必须的, 但可惜的是, Passwor

  • C#窗体贴边自动隐藏组件 By Red_angelX

    5/15/2007 7:25:00 PM

    摘要: 靠边隐藏效果:当程序移动到显示的边界时,程序自动隐藏起来,留一条小边在外面,很酷的效果吧. 先介绍下实现原理,靠边隐藏无非就是判断窗体的位置和屏幕的四边的关系,当上左右三个

  • MSSQL之系統函數NEWID()我之理解與學習

    12/18/2009 1:24:00 PM

    阅读: 408 评论: 4 作者: ☆用心生活☆ 发表于 2009-12-18 10:46 原文链接 大家也許對MS SQL的隨機函數NEWID應該不會陌生吧,昨天同事問我在LINQ中怎么