2014-07-02 18 views
1

我使用的是Grails 2.3.8,最近我开始修复一些过时的测试,这些测试在过去的几个Grails 2.3.x版本中停止工作。Grails控制器单元测试通过命令行而不是Intellij

在进行Spock控制器测试期间,以及在使用来自Controller的域实例进行响应时,根据测试执行环境,对模型的断言失败/通过。特别是,在使用Grails控制台vs IntelliJ(Junit)执行测试时,模型上的域实例具有不同的属性名称。

我一直在对冲它在我的斯波克的规格使得这类说法在我然后预计块:

void "show action correctly handles a valid instance"() { 

    given: "a valid domain instance" 

     def myDomainObject = MyDomainClass.build() 

    when: "calling the show action with a valid domain instance" 

     controller.show(myDomainObject) 

    then: "respond to the show view with the domain instance set on the model" 

     view == ‘show’ 
     // the property name ends with ‘Instance’ in one env and not in the other       
     model.myDomainObjectInstance ?: model.myDomainObject == myDomainObject 
} 
+0

请投票的人请解释原因吗?不要试图争论,但我想知道我的帖子中是否有错误或缺失,可能有助于他人理解和回应。 – ZDJ

+0

我会仔细检查你是否在这两种环境中使用相同的grails版本。很容易导致不匹配。 – allTwentyQuestions

+0

我希望这是件小事,但我有一种感觉,它与在Intellij中配置Grails环境的方式有关。我们的生产环境中一切运行良好。一旦我在下一个Sprint中获得一些时间,我将尝试解决问题的根源。 – ZDJ

回答

0

不知道这可能是一个问题,但我认为您应该将控制器的响应保存在变量中,并使用renderArgs变量访问视图:

void“show action correctly properly a valid instance”(){

given: "a valid domain instance" 

    def myDomainObject = MyDomainClass.build() 

when: "calling the show action with a valid domain instance" 

    def model = controller.show(myDomainObject) 

then: "respond to the show view with the domain instance set on the model" 

    renderArgs.view == ‘show’ 
    // the property name ends with ‘Instance’ in one env and not in the other       
    model.myDomainObjectInstance ?: model.myDomainObject == myDomainObject 

}

不知道是否这就是问题所在。

+0

该模型不为null并包含该实例,并且assertion'view =='show''通过。问题是,从命令行运行时,用于访问实例的属性名称(在模型上)是'myDomainObject',但在IntelliJ中运行时,它是'myDomainObjectInstance'。就目前而言,有几十个这样的测试通过,但只是因为我明确断言'myDomainObject'等于'model.myDomainObjectInstance'*或*'model.myDomainObject'。我不清楚为什么会发生这种情况。 – ZDJ

+0

我会尝试检查intellij项目中的库和命令行选项 –

+0

之间的区别按照我上面的评论,我认为我倾向于这样的东西。 – ZDJ