开发者

asp.net mvc integration test

Hi Im doing TDD for an asp.net mvc project, I need to be able to do end to end testing for sending a request to the controller action all the way to the repository. I have tried using the code here but unfortunately I can't get this to run and I'm running out of time, does anyone know any other way to fake an http request and populate request post parameters in a test scenario?

My controller action is as follows:

[HttpPost]
public ActionResult CreateUser(User user)
{
}

So I need to basically do an http request to populate this User obje开发者_开发百科ct and hopefully save it to a test repository.


As you posted the link I'll take an extract from Steve Sanderson's blog:

Integration tests test your entire software stack working together. These tests don’t mock or fake anything (they use the real database, and real network connections) and are good at spotting if your unit-tested components aren’t working together as you expected. In general, it’s best to put most of your effort into building a solid suite of unit tests, and then adding a few integration tests for each major feature so you can detect any catastrophic incompatibilities or configuration errors before your customers do.

You shouldn't be faking HTTP requests at this stage as an integration test inherantly tests every component together.

Try some type of browser automation framework:

  • http://blog.stevensanderson.com/2010/03/30/using-htmlunit-on-net-for-headless-browser-automation/
  • http://www.codeproject.com/KB/cs/mshtml_automation.aspx


If you want to do full integration testing, then test your application from user prospective. Create test cases like:

  1. Log in as admin
  2. Go to Users page
  3. Add User with name "User1"
  4. Check that user with name "User1" listed in the Users grid.

And automate such tests using Selenium or Watin. See example here


You may also want to take a look at the Verde framework. Semantically the tests look similar to Steve Sanderson's MvcIntegrationTestFramework with the key difference being that Verde executes tests in the context of your actual IIS AppDomain (via a browser-based test runner) rather than a programmatically created one. This provides a couple of advantages: First it is a more realistic emulation of your actual application's configuration, network topology, security settings, etc. Secondly you can automate running of the tests as a post-deployment step or could even run the tests automatically as part of application monitoring in production. Here is an example Verde test taken from the MvcMusicStore sample that is included in the source code on GitHub:

[IntegrationTest]
public void Index_Load_ExpectedHtml()
{
  // Get a product to load the details page for.
  var album = storeDB.Albums
    .Take(1)
    .First();

  using (var scope = new MvcExecutorScope("Store/Details/" + album.AlbumId))
  {
    Assert.AreEqual(200, scope.HttpContext.Response.StatusCode);
    Assert.IsTrue(scope.Controller is StoreController);
    Assert.AreEqual("Details", scope.Action);

    var model = scope.Controller.ViewData.Model as Album;
    Assert.IsNotNull(model);
    Assert.AreEqual(album.AlbumId, model.AlbumId);

    Assert.IsFalse(String.IsNullOrEmpty(scope.ResponseText));

    // Load the ResponseText into an HtmlDocument
    var html = new HtmlDocument();
    html.LoadHtml(scope.ResponseText);

    // Use ScrappySharp CSS selector to make assertions about the rendered HTML
    Assert.AreEqual(album.Title, html.DocumentNode.CssSelect("#main h2").First().InnerText);
  }
}

There is a NuGet package which makes it very easy to add to your MVC project.

  • http://dvonlehman.github.com/Verde/
  • https://nuget.org/packages/Verde/0.5.1
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜