开发者

OpenRasta unit testing

I’m about to start work on an OpenRasta project (an xml over http web service). OpenRasta looks great but unfortunately worked examples seem few and far between on the internet. Looking at the test side of the project, if my handlers are returning strongly typed objects (not OperationResult), i.e.:

public class PersonHandler
...
 public Person Get(int id)
 {
 ...

How can I test for http status codes? (For example if the handler throws an uncaught exception). I’m not sure what level the t开发者_StackOverflow中文版ests pitch in at, and what needs mocking (using moq btw)

Any help appreciated, particularly coded examples!


I faced the same problem, and ended up writing my tests as integration tests at a much higher level, actually making real REST/HTTP calls through a simple HttpWebRequest client. This allowed me to check the HTTP response headers / status codes and double-check the JSON/XML serialization from the client's perspective, which was just as important as whether or not the operations succeeded.

I started by returning OperationResult from all my handlers, and used these to wrap the strongly-typed objects. My handlers all inherit from a base class with a few helper methods that make it easier to return a custom error with a user-friendly error message. The more I coded this up, the more my handlers resembled a ASP.NET MVC controller. e.g.:

    public OperationResult GetById(int id)
    {
        try
        {
            // do stuff here
            return OKResult( // some strongly-typed resource );
        }
        catch(SomeException ex)
        {
            return BadRequestResult(SomeErrorCode, ex.Message);
        }
    }

Then in the test client, it's pretty easy to just check the HTTP status code. Obviously this doesn't help much with mocking. I'm not sure what the best solution is, in fact I've favorited this question in the hope that someone answers it better than I can - but this has worked pretty well for me so far.


The handler is just a class--ideally with minimal dependencies--so your unit tests can just test the isolated logic in the class.

If you want to test for status codes, I recommend (based on very little experience!) using OpenRasta self-hosting.

Here's a test (somewhat changed) that I wrote recently:

    [TestMethod]
    public void POST_with_inaccurate_contentLength_returns_405()
    {
        var resource = GetResource();

        IRequest request = new InMemoryRequest
        {
            HttpMethod = "POST",
            Uri = new Uri("http://localhost/Resource"),
        };
        request.Headers.ContentLength = 16;  //wrong!

        request.Entity.Stream.Write(resource.Content, 0, resource.Content.Length);

        var response = _host.ProcessRequest(request);

        Assert.AreEqual(405, response.StatusCode);
    }

I should add that the host is set up in the TestInitialize method as such:

        _host = new InMemoryHost(new Configuration());
        _host.Resolver.AddDependencyInstance(typeof(IFileResourceRepository), _repository, DependencyLifetime.Singleton);

...and is cleaned up in the TestCleanup method.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜