grails サービスをテストするための単体テストを作成しようとしています。次のテストがあります
@TestFor(ActivityProcessorService)
@Mock([ActivityProcessorService, Activity])
class ActivityProcessorServiceTests extends GrailsUnitTestCase{
void setUp() {
}
void tearDown() {
// Tear down logic here
}
void testGenerateDescription() {
def activity = new Activity(
//new activity details
)
def service = mockFor(ActivityProcessorService)
def description = service.generateDescription(activity)
assert description == "something..."
}
}
私の問題は、Activity
オブジェクトを作成して必要なすべてのフィールドにデータを入力するときに、 などの他のいくつかのオブジェクトを作成する必要があることですUser
。Task
これらのオブジェクトは非常に大きくなる可能性があり、作成が必要な影響があります。オブジェクトなど
Activity
オブジェクトTask
を作成する方法はありUser
ますか?
例えば
def activity = new Activity(
task: new Task(),
user: new User(),
... and so on
)
Task と User は、次のような完全なオブジェクトを作成するのではなく、モックアップされます
def activity = new Activity(
task: new Task(
title : "task title"
description : "task description"
... and so on
),
user: new User(
firstName : "john",
lastName : "smith",
... and so on
),
... and so on
)
これは、このような小さくて単純なテストを作成するためのオーバーヘッドがかなり大きくなるためです。