Mock httpcontext for MVC Testing

Source: Internet
Author: User
Tags log4net

It is too complicated to simulate httpcontext for testing-driven development on the web.

The Moq framework provides powerful simulation capabilities. However, you still need to manually simulate an httpcontext object.

For this reason, I wrote a method to complete the job. Log4net is also used to output its working conditions.

/// <Summary> /// create a context simulation object /// at least, you need to support /// request /// apprelativecurrentexecutionfilepath, /// applicationpath // pathinfo // response /// applyapppathmodifier /// </Summary> /// <returns> </returns> private Moq. mock <system. web. httpcontextbase> createhttpcontext () {log4net. ilog log = log4net. logmanager. getlogger ("createhttpcontext"); string applicationpath = "/"; string pathinfo = ""; string apprelativ Ecurrentexecutionfilepath = "~ /"; Var contextmock = new Moq. mock <system. web. httpcontextbase> (); contextmock. setup (C => C. request. apprelativecurrentexecutionfilepath ). returns (apprelativecurrentexecutionfilepath ). callback () => log. info ("calling apprelativecurrentexecutionfilepath"); contextmock. setup (C => C. request. applicationpath ). returns (applicationpath ). callback () => log. info ("calling applicationpath"); contextmock. setup (rc => RC. request. pathinfo ). returns (pathinfo ). callback () => log. info ("calling pathinfo"); contextmock. setup (rc => RC. response. applyapppathmodifier (Moq. it. isany <string> ())). returns (string S) => S ). callback (string S) => log. infoformat ("calling applyapppathmodifier: {0 }. ", S); Return contextmock ;}

Although this method has been able to complete the test I need, I hope to refine it to get a more general mock method.

Soon, I found that this job was introduced by Scott hanselman a long time ago, and I even wrote about how to provide IT UNDER DIFFERENT mock frameworks. But the author of The Moq version is not him, but another Daniel cazzulino, which you can find here: Daniel cazzulino's blog: http://blogs.clariusconsulting.net/kzu/.

However, his article was written in 2008, and he is too old. At that time, the MVC preview2 and Moq were born at that time. It seems that I am too ignorant.

After returning to our topic, after such a long period of time, Moq has made some changes. After using the latest Moq syntax, we found that its code could not pass the test.

Compared with me, I found that the applyapppathmodifier method in response is missing, and it will be normal after it is added.

The following is the modified Code, hoping to make it easier for you.

Using system; using system. web; using system. text. regularexpressions; using system. io; using system. collections. specialized; using system. web. MVC; using system. web. routing; using Moq; namespace unittests {public static class mvcmockhelpers {public static httpcontextbase fakehttpcontext () {var context = new mock 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.