Skip to content

Instantly share code, notes, and snippets.

@wwwlicious
Last active April 14, 2016 15:18
Show Gist options
  • Save wwwlicious/97e1de53801e459faba57ef025003dda to your computer and use it in GitHub Desktop.
Save wwwlicious/97e1de53801e459faba57ef025003dda to your computer and use it in GitHub Desktop.
namespace ServiceStackCacheServiceTesting
{
using System;
using ServiceStack;
using ServiceStack.Caching;
using ServiceStack.Testing;
using ServiceStack.Web;
using Xunit;
public class AppHostFixture : IDisposable
{
public AppHostFixture()
{
var config = new HostConfig { WebHostUrl = "http://localhost" };
Host = new BasicAppHost(typeof(...serviceassembly...).Assembly)
{
TestMode = true,
Config = config,
};
Host.Register<ICacheClient>(new MemoryCacheClient());
Host.Init();
}
public BasicAppHost Host { get; set; }
public T SetTestService<T>(T service, IRequest request = null) where T : Service
{
service.Request = request ?? new MockHttpRequest();
Host.Register(service);
return Host.Resolve<T>();
}
public void Dispose()
{
Host.Dispose();
}
public T FromCachedResult<T>(object any)
{
return any.ToString().FromJson<T>();
}
}
[CollectionDefinition("AppHost")]
public class AppHostCollection : ICollectionFixture<AppHostFixture>
{
// marker class for tests that require apphost.init
// http://xunit.github.io/docs/shared-context.html
}
[Collection("AppHost")]
public abstract class CacheServiceTests
{
private readonly AppHostFixture fixture;
private readonly CachedService service;
public CacheServiceTests(AppHostFixture fixture)
{
this.fixture = fixture;
service = fixture.SetTestService(new CachedService());
}
[Fact]
public void CanGetCachedResponse()
{
var any = service.Any(new MyDto { Text = "What goes in..." });
var result = fixture.FromCachedResult<MyDto>(any);
result.Text.Should().Be("Must come out");
}
}
public class CachedService : Service
{
public object Any(MyDto request)
{
return Request.ToOptimizedResultUsingCache(
Cache,
request.Text,
() => new MyDto { Text = "Must come out" });
}
}
public class MyDto : IReturn<MyDto>
{
public string Text { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment