서비스 스텍(ServiceStack) 테스트코드 작성

서비스 스텍(ServiceStack) 테스트코드 작성

서비스 스텍을 사용해서 개발을 했으면 아래같은 코드를 사용해 간단하게 테스트 해볼수 있다.

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
namespace io.github.sejoung.servicestack.test
{
[TestFixture]
class ServiceStackTest
{
private ServiceStackHost appHost;

[OneTimeSetUp]
public void OneTimeSetUp()
{
appHost = new BasicAppHost().Init();
var container = appHost.Container;

container.Register<IDbConnectionFactory>(
new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider));

container.RegisterAutoWiredAs<RockstarRepository, IRockstarRepository>();

container.RegisterAutoWired<SimpleService>();

using (var db = container.Resolve<IDbConnectionFactory>().Open())
{
db.DropAndCreateTable<Rockstar>();
db.InsertAll(SeedData);
}
}

[OneTimeTearDown]
public void OneTimeTearDown() => appHost.Dispose();


[Test]
public void Using_in_memory_database()
{
//Resolve the autowired service from IOC and set Resolver for the base class
var service = appHost.Container.Resolve<SimpleService>();

var rockstars = service.Get(new FindRockstars { Aged = 27 });

rockstars.PrintDump(); //Print a dump of the results to Console

Assert.That(rockstars.Count, Is.EqualTo(SeedData.Count(x => x.Age == 27)));

var status = service.Get(new GetStatus { LastName = "Vedder" });
Assert.That(status.Age, Is.EqualTo(48));
Assert.That(status.Alive, Is.True);

status = service.Get(new GetStatus { LastName = "Hendrix" });
Assert.That(status.Age, Is.EqualTo(27));
Assert.That(status.Alive, Is.False);

Assert.Throws<HttpError>(() =>
service.Get(new GetStatus { LastName = "Unknown" }));
}


}
}

참조