Skip to content

Instantly share code, notes, and snippets.

@derwolfe
Last active December 14, 2015 09:29
Show Gist options
  • Save derwolfe/5064944 to your computer and use it in GitHub Desktop.
Save derwolfe/5064944 to your computer and use it in GitHub Desktop.
Setting up testing for ServiceStack 3.9... project. I was having trouble figuring out how to create my test with service stack. After looking at the endpoint tests from the service stack examples I decided to organize my tests using the following layout.
using NUnit.Framework;
using ServiceStack.ServiceClient.Web;
using example_proj.Services;
namespace Tests
{
[TestFixture]
public class ServiceTests
{
private TestAppHost _appHost;
public JsonServiceClient CreateClient()
{
return new JsonServiceClient(Config.AbsoluteBaseUri);
}
[TestFixtureSetUp]
public void TestSetup()
{
_appHost = new TestAppHost();
_appHost.Init();
_appHost.Start(Config.AbsoluteBaseUri);
}
[TestFixtureTearDown]
public void TestTearDown()
{
_appHost.Stop();
_appHost.Dispose();
}
[Test]
public void TestGetAllThings()
{
var client = CreateClient();
var response = client.Get<ThingsResponse>("things");
Assert.IsTrue(response.Things.Count > 0);
}
[Test]
[ExpectedException("ServiceStack.ServiceClient.Web.WebServiceException")]
public void TestThingDoesNotExist()
{
var client = CreateClient();
var response = client.Get<ThingsResponse>("thing/nope");
Assert.IsTrue(response.ResponseStatus.ErrorCode == "thing not found");
}
}
}
using ServiceStack.OrmLite;
using ServiceStack.OrmLite.SqlServer;
using ServiceStack.WebHost.Endpoints;
using example_proj.Services;
namespace Tests
{
public class TestAppHost : AppHostHttpListenerBase
{
public TestAppHost() : base("testing http listener", typeof(ExampleService).Assembly) {}
public override void Configure(Funq.Container container)
{
const string connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=test-db;Integrated Security=SSPI";
container.Register<IDbConnectionFactory>(c =>
new OrmLiteConnectionFactory(connectionString,
SqlServerOrmLiteDialectProvider.
Instance));
}
}
}
@derwolfe
Copy link
Author

derwolfe commented Mar 1, 2013

Just noticed the spacing for the directory structure didn't come through; here it is again:

/Solution
/Example_proj
/Example_proj/Global.asax.cs - containing app init and AppHost
/Example_proj/ServiceModels

/Tests
/Tests/TestAppHostBase.cs (based on self hosted AppHostListenerBase)
/Tests/ServiceTest.cs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment