Towards testing OpenRasta views (OR 2.0.3, .NET 4, VS 2010)
My employer is a .NET shop. Despite my open source upbringing, I’ve been getting to grips with some of the newer .NET technologies, OpenRasta for exposing objects RESTfully, Fluent NHibernate for simple object-RDBMS mapping. Some pretty cool stuff coming from the ALT.NET scene.
I’m building a RESTful XML service with OpenRasta. I encountered a couple of problems that weren’t covered by the community documentation so I’ve written them up here. As such this is not a complete introduction. See this question on StackOverflow about getting started with OpenRasta for help on that front.
A quick note about Web.config
The minimal changes to Web.config suggested by the first site tutorial wasn’t enough for me in VS 2010. However, borrowing the Web.config from OpenRasta.Demo (included in the OR repo) and hacking it a little bit worked a treat. Here is the complete Web.config for an OR 2.0.3 project hosted on ASP.NET in VS2010 that’s working for me right now.
A note about NUnit versions
OpenRasta.DI.Unity.Tests.Unit introduces a dependency on nunit.framework.dll version 2.5.1.9189. If you’re using a different version, you can redirect the binding in App.config. See Resolving Dependent .NET Assembly Version Conflicts for more info.
Testing views
Django’s test client makes it easy to test a view.
>>> from django.test.client import Client
>>> c = Client()
>>> response = c.get("/retailer/1")
>>> response.content
'<?xml version="1.0" encoding="utf-8"?>...'</code></pre>
I want to do the same thing to my OpenRasta views.
- Set up a lightweight environment with 1 or 2 lines of code.
- Pass an URL to a method which returns the view output.
- Parse response content and inspect data contained therein.
I would prefer not to have to set up a dev web server or use Selenium or Twill or some other tool to test my view code. Although those are valid integration tests, they’re complicated and slow. My purpose is just to demonstrate that I’ve wired my domain model and OpenRasta together correctly, so I want to test the shortest code path.
OpenRasta provides InMemoryHost
which looks like it should solve the problem nicely. OpenBastard, OpenRasta’s suite of regression tests, also provides some tools for solving this problem. Sadly, both are undocumented and work in progress at the time of writing.
After spending some time looking through the source and asking questions on the mailing list, I wasn’t able to create a test environment using either InMemoryHost
or OpenBastard. I’ll update this post as and when I make any progress.
However, in the meantime, here is an integration test that passes a request to OR through IIS running on localhost.
[TestFixture]
public class RetailerTests
{
[Test]
public void GetRetailer_RetailerExists_RetailerRepresentationReturned()
{
var uri = new Uri("http://localhost/retailers/1");
var webRequest = (HttpWebRequest)WebRequest.Create(uri);
webRequest.Method = "GET";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
Retailer retailer;
DataContractSerializer dcs = new DataContractSerializer(typeof(Retailer));
using (var response = webRequest.GetResponse())
{
retailer = (Retailer)dcs.ReadObject(response.GetResponseStream());
}
Assert.AreEqual(retailer.Id, 1);
Assert.AreEqual(retailer.Name, "Retailer");
Assert.AreEqual(retailer.Products.Count, 1);
}
}
Based on code posted to the OpenRasta mailing list by David Lawton.