简体   繁体   English

通过Grails测试通过Controller中的Domain类访问db中的数据

[英]Accessing the data in db via Domain class in Controller via grails testing

def index() { 
  //println params
    String params_key =params['key']
    def c =  get_value(params_key)
    def resp = ['key': params_key, 'value':c]
    render resp as JSON
}
private static hash_conv(String value)
{
  def matches = Eval.me(value.replace(':','').replace('{','[').replace('=>',':').replace('#','//').replace('}',']'))
  return matches
}

private get_value(String key, default_value=null){
  def app_preferences = get_preferences()
  def result = app_preferences[key]
   if (result == null) {
    result = default_value
   }

  return result
}

private get_preferences(Boolean mobile_app = false){
  def all_app_preference =  AppPreferences.all
  def mapped_value = [:]
  def all_app = all_app_preference.each{obj -> mapped_value << get_preference(obj)}
  return mapped_value
}

private static get_preference(AppPreferences preference){
  def value_type = preference.value_type.toLowerCase()
  def val = value_type == 'integer' ? preference.value.toBigInteger() : (value_type == 'boolean' ? (preference.value == 'true' || preference.value == '1' ? true : false):(value_type == 'array' ? preference.value.split(',') : (value_type == 'hash' ? hash_conv(preference.value) :(value_type == 'json' ? new JsonSlurper().parseText(preference.value) : preference.value.toString()))))
  def map_value = [:]
  map_value[preference.preference_key] = val
  return map_value
}

Here I am using AppPreferences domain . 在这里,我正在使用AppPreferences域。 It is returning some value on localhost.But when I test it in grails it is returning Null. 它在localhost上返回了一些值,但是当我在grails中对其进行测试时,它返回了Null。 My test code as follows: 我的测试代码如下:

@TestFor(AppPreferencesController)
@Mock( [AppPreferences] )
//controller.session.SPRING_SECURITY_CONTEXT = [authentication:[principal:[id: 'blah']]]
class AppPreferencesControllerSpec extends Specification {

    def setup() {
    }

    def cleanup() {
    }

    /*void "test something"() {
        expect:"fix me"
            true == false
    }*/
    void test_for_index()
    {
        when:
            controller.session.SPRING_SECURITY_CONTEXT = [authentication:[principal:[id: 'blah']]]
            params.key = 'role_to_feature_map'
            controller.index()
        then:
            1 == 1
            2 == 2
            println response.text
    }
}

the response.text is Returning as null. response.text返回为空。 In local host it is returning a hash value. 在本地主机中,它返回一个哈希值。

Perhaps: 也许:

void test_for_index() {
    when:
        controller.session.SPRING_SECURITY_CONTEXT = [authentication:[principal:[id: 'blah']]]
        controller.params.key = 'role_to_feature_map'  <-- the params attached to the controller
        controller.index()
    then:
        1 == 1
        2 == 2
        println response.text
}

Tests typically run against a different database than development, or production. 测试通常针对与开发或生产不同的数据库运行。 Your test (I assume it is a unit test) will need to mock your AppPreferences domain class. 您的测试(我假设这是一个单元测试)将需要模拟您的AppPreferences域类。 A unit test is just that, only the unit of code being tested. 单元测试就是这样,只有被测试的代码单元。 There isn't a Grails application surrounding the tested code. 测试的代码周围没有Grails应用程序。

I would probably add a given: section to the current test, and initialize the entities in the AppPreferences domain class there. 我可能会在当前测试中添加一个给定的:部分,并在那里初始化AppPreferences域类中的实体。

given:
    def appPref1 = new AppPreferences("whatever you must set to pass constraints").save(flush:true)
    controller.session.SPRING_SECURITY_CONTEXT = [authentication:[principal:[id: 'blah']]]
    controller.params.key = 'role_to_feature_map'

when:
    controller.index()

then:
    1 == 1
    2 == 2
    println response.text

Personal opinion: The line of code below is unreadable. 个人观点:下面的代码行不可读。 It would never pass code review where I work. 在我工作的地方,它永远不会通过代码审查。 Try a switch statement. 尝试使用switch语句。

def val = value_type == 'integer' ? def val = value_type =='整数' preference.value.toBigInteger() : (value_type == 'boolean' ? (preference.value == 'true' || preference.value == '1' ? true : false):(value_type == 'array' ? preference.value.split(',') : (value_type == 'hash' ? hash_conv(preference.value) :(value_type == 'json' ? new JsonSlurper().parseText(preference.value) : preference.value.toString())))) preference.value.toBigInteger():(value_type =='boolean'?(preference.value =='true'|| preference.value =='1'?true:false):( value_type =='array'? .value.split(','):(value_type =='hash'?hash_conv(preference.value):(value_type =='json'?new JsonSlurper()。parseText(preference.value):preference.value.toString ()))))

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

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