如何在使用Spock功能测试Grails时设置对象 [英] How to set objects while functional testing grails with Spock

查看:135
本文介绍了如何在使用Spock功能测试Grails时设置对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class User {
字符串用户名
字符串密码
$ b $ def userHelper

静态限制= {
用户名(可空:false,空白:false)
密码可空:false,空:false ,验证器:{pwd,userInstance - >
return userInstance.userHelper.validatePassword(pwd)
}
}
}

userHelper 由以下内容在我的 resources.groovy

  beans = {
userHelper(UserHelper)
}

当我从浏览器测试我的应用程序时,一切运行良好。但是,我在尝试为此编写功能测试时遇到异常。



错误说:无法在空对象上调用方法validatePassword()



所以我假设当我运行我的功能测试用例时没有设置 userHelper



我的测试用例如下所示:

  @TestFor(UserController )
@Mock([User])

class UserControllerSpecification extends Specification {


defsave user(){
given :
request.contentType =application / json
request.JSON =
{user:
{
username:somtething
密码:某事
}
}

时:
controller.save()

然后:
User.count()== 1
}
}

更新



控制器:

  class UserController { 
def userService
def save(){
def user = new User(params?.user)
request.withFormat {
json {
if(user.validate())
userService.processUser()
//做一些事
else
//做别的事
}
}
}
}

问题




  • 如何设置 userHelper 属性用户在运行我的测试之前的域

  • 在运行我所有的功能和集成测试用例之前,我在 Bootstrap.groovy 中运行我的代码?


  • defineBeans
    ?由于这是一个单元测试,你必须明确定义bean。这通常在JUnit中完成


      defineBeans {
    userHelper(UserHelper){bean - >
    //如果您在助手中定义了其他bean
    bean.autowire = true
    }
    }

    您可以在setup方法中使用 defineBeans ,以使其适用于所有测试用例。

      @Before 
    void setup(){
    defineBeans {
    userHelper(UserHelper){bean - >
    //如果您在助手中定义了其他bean
    bean.autowire = true
    }
    }
    }

    更新:

    直接在一个单元测试(一个扩展规范的测试)中注入一个spring bean

    $ p $ class UserControllerSpecification extends Specification {
    def userHelper //注入spock

    defsave user(){
    ......... .......
    }
    }


    I've got a sample Domain such as this

    class User {
        String username
        String password
    
        def userHelper
    
        static contraints = {
            username(nullable: false, blank: false)
            password nullable: false, blank: false, validator: {pwd, userInstance ->
                return userInstance.userHelper.validatePassword(pwd)
            }
        }
    }
    

    the userHelper is being injected by the following in my resources.groovy

    beans = {
        userHelper(UserHelper)
    }
    

    When I test my application from the browser, everything runs fine. However, I get an exception while trying to write functional tests for this.

    The error says: Cannot invoke method validatePassword() on null object

    So I'm assuming that userHelper is not being set when I run my functional test case.

    My test case looks like this:

     @TestFor(UserController)
     @Mock([User])
    
    class UserControllerSpecification extends Specification {
    
    
        def "save user" () {
            given:
                request.contentType = "application/json"
                request.JSON = """
                                {user:
                                    {
                                        username: "somtething"
                                        password: "something"
                                    }
                                }
                            """
            when: 
                controller.save()
    
            then:
                User.count() == 1
        }
    }
    

    Update

    Controller:

    class UserController {
        def userService
        def save() {
            def user = new User(params?.user)
            request.withFormat {
                json {
                    if(user.validate())
                      userService.processUser()
                      //do something
                    else
                      //do something else
                }
            }
        }
    }
    

    Questions

    • How can I set the userHelper property on User domain prior to running my tests?
    • How can I run my code in Bootstrap.groovy prior to running all my functional and integration test cases?

    解决方案

    Have you tried using defineBeans? Since this is a unit test you have to explicitly define the beans. This how it is normally done in JUnit

    defineBeans{
        userHelper(UserHelper){bean ->
            //in case you have other beans defined inside helper
            bean.autowire = true 
        }
    }
    

    You can defineBeans in the setup method to make it available for all the test cases.

    @Before
    void setup(){
        defineBeans{
            userHelper(UserHelper){bean ->
                //in case you have other beans defined inside helper
                bean.autowire = true 
            }
        }
    }   
    

    UPDATE:

    Seems like in case of spock you can directly inject a spring bean inside a Unit Test (test which extends Specification)

    class UserControllerSpecification extends Specification {
        def userHelper //injected here in case of spock
    
        def "save user" () {
         ................
        }
    }
    

    这篇关于如何在使用Spock功能测试Grails时设置对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆