2011-03-11 4 views
0

내 응용 프로그램에서 도메인 클래스에서 작동하도록되어 있지만 작동하는 도메인 클래스 유형을 신경 쓰지 않는 컨트롤러가 있습니다. 유닛 테스트를 작성하여 올바르게 응답하는지 확인하고 싶지만 도메인 클래스가 변경되거나 제거 된 경우 애플리케이션의 도메인 클래스에 연결하고 싶지 않습니다. 다음과 같은 것 :컨트롤러 유닛 테스트에서 도메인 클래스 종속성 제거

void testReadNoItems() { 
    mockDomain(Item) 

    controller.params["class"] = "DefaultGrailsDomainClass" 
    controller.params.xaction = "read" 
    controller.index() 

    def json = JSON.parse(controller.response.contentAsString) 

    assert json.metaData.root == "data" 
    assert json.metaData.totalProperty == "total" 
    assert json.metaData.successProperty == "success" 
    assert json.metaData.idProperty == "id" 
    assert json.metaData.fields[0].id == "int" 
    assert json.metaData.fields[1].name == "string" 
    assert json.data == [] 
    assert json.total == 0 
} 

항목 도메인 클래스에 대한 종속성을 제거하는 방법이 있습니까?

답변

1

Item의 모든 참조를 컨트롤러에서 제거하는 경우 ItemService (트랜잭션)을 만들어 컨트롤러에 삽입 할 수 있습니다. 거기에서 ItemService을 상대적으로 고정시키고 컨트롤러에서 조롱 할 수 있습니다.

테스트 :

protected void setUp(){ 
    controller.itemService = [retrieveItems: { arg -> return [new Item()] }] as ItemService 
} 
+0

동의 - 컨트롤러는 도메인 객체를 호출 서비스를 호출해야합니다. – sourcedelica

관련 문제