简体   繁体   English

如何使用Spock测试Grails服务?

[英]How to test Grails service using Spock?

I have a EncouragementService.groovy with following method 我有一个EncouragementService.groovy ,方法如下

  class EncouragementService {
   def stripePaymentService 

   def encourageUsers(List<User> users){
     if(null != users && users.size()>0){
      for(User user : users){
        //logic
        stripePaymentService.encourage(user)
        //
      }
     }   
   }
  }

To test above code in JAVA universe, using JUnit I would first create two or three users in setup. 要在JAVA Universe中测试上面的代码,使用JUnit我首先要在安装程序中创建两个或三个用户。 Pass the list of users to encourageUsers(...) method and check whatever I want with the result. 将用户列表传递给encourageUsers(...)方法并检查我想要的结果。

How can I achieve the same thing here in grails, 我怎样才能在grails中实现同样的目标,

import com.github.jmkgreen.morphia.Datastore;

@TestFor(EncouragementService)
class EncouragementServiceSpec {

  def morphiaService = new MorphiaService()

  void testEncourageUsers() {
        List<User> users = createUsers();
        encouragementService.(users)  
            //
  }

  def createUsers(){
       Datastore datastore = morphiaService.dataStoreInstance()
       def user = new User()
       user.setName("Prayag Upd")
       //
       datastore.save(user)
       [user]
  }
}

I am using spock:0.7 我正在使用spock:0.7

plugins {
    test(":spock:0.7") { exclude "spock-grails-support" }
}

Service class can be optimized as below: 服务类可以优化如下:

class EncouragementService {
   def encourageUsers(List<User> users){
       if(users){ //Groovy truth takes care of all the checks
          for(user in users){
            //logic
          }
       }   
   }
}

Spock Unit Test: Spock单元测试:
Spock takes testing to whole another level, where you can test the behavior (adheres to BDD). Spock将测试提升到另一个级别,您可以在其中测试行为(遵循BDD)。 The test class would look like: 测试类看起来像:

import spock.lang.*

@TestFor(EncouragementService)
@Mock(User) //If you are accessing User domain object.
class EncouragementServiceSpec extends Specification{
  //def encouragementService //DO NOT NEED: mocked in @TestFor annotation

  void "test Encourage Users are properly handled"() {
      given: "List of Users"
          List<User> users = createUsers()

      when: "service is called"
          //"service" represents the grails service you are testing for
          service.encourageUsers(users) 

      then: "Expect something to happen"
          //Assertion goes here
  }

  private def createUsers(){
       return users //List<User>
  }
}

Use the build-test-data plugin to build the users. 使用build-test-data插件构建用户。

@TestFor(EncouragementService)
@Build(User)
class EncouragementServiceSpec extends Specification {

  def "encourage users does x"() {
    given:
    def users = createUsers();

    when:
    service.encourageUsers(users)  

    then:
    // assert something
  }

  def createUsers(){
    [User.build(), User.build(), User.build()]
  }
}

I've also made a couple of changes to the code to make it a proper spock specification. 我还对代码进行了一些更改,使其成为一个合适的spock规范。 Your test has to extend Specification and you might want to familiarize yourself with Spock's keywords . 您的测试必须扩展规范,您可能希望熟悉Spock的关键字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM