In my previous post I showed you how I used ServiceStack to do the integration tests with Easyhttp.

I also used a different port for each test and thus a different instance of ServiceStack for each of these tests.

Of course The Boss was not happy with this.

So I went for a look on how to do this the better way.

And I found this on Stackoverflow. So thanks to Jason Watts for helping me along.

So the ServiceStackHost.cs file is still the same as before. But I created this class.

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using Machine.Specifications;
 
namespace EasyHttp.Specs.Helpers
{
    public class DataSpecificationBase : IAssemblyContext
    {
        private ServiceStackHost _appHost;
        private int _port;
 
        void IAssemblyContext.OnAssemblyComplete()
        {
            _appHost.Dispose();    
        }
 
        void IAssemblyContext.OnAssemblyStart()
        {
            _port = 16000;
            var listeningOn = "http://localhost:" + _port + "/";
            _appHost = new ServiceStackHost();
            _appHost.Init();
            _appHost.Start(listeningOn); 
        }
 
    }
}
using Machine.Specifications;

namespace EasyHttp.Specs.Helpers
{
    public class DataSpecificationBase : IAssemblyContext
    {
        private ServiceStackHost _appHost;
        private int _port;

        void IAssemblyContext.OnAssemblyComplete()
        {
            _appHost.Dispose();    
        }

        void IAssemblyContext.OnAssemblyStart()
        {
            _port = 16000;
            var listeningOn = "http://localhost:" + _port + "/";
            _appHost = new ServiceStackHost();
            _appHost.Init();
            _appHost.Start(listeningOn); 
        }

    }
}

And I removed the apphost field and init and teardwon code from the tests. So a test now looks like this.

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[Subject("HttpClient")]
    public class when_making_a_GET_request_with_valid_uri
    {
        Establish context = () =>
        {
            httpClient = new HttpClient();
        };
 
        Because of = () =>
        {
            httpResponse = httpClient.Get("http://localhost:16000");
 
        };
 
        It should_return_body_with_rawtext =
            () => httpResponse.RawText.ShouldNotBeEmpty();
 
    
        static HttpClient httpClient;
        static HttpResponse httpResponse;
    }
[Subject("HttpClient")]
    public class when_making_a_GET_request_with_valid_uri
    {
        Establish context = () =>
        {
            httpClient = new HttpClient();
        };

        Because of = () =>
        {
            httpResponse = httpClient.Get("http://localhost:16000");

        };

        It should_return_body_with_rawtext =
            () => httpResponse.RawText.ShouldNotBeEmpty();

    
        static HttpClient httpClient;
        static HttpResponse httpResponse;
    }

It has no clue about apphost.

And now the Boss is happy.

And you can find all the code on Github.

So now I know a bit more about mspec, servicestack and easyhttp. One is never to old to learn.