Accessing the data in db via Domain class in Controller via grails testing

筅森魡賤 提交于 2019-12-25 08:43:06

问题


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 . It is returning some value on localhost.But when I test it in grails it is returning 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. In local host it is returning a hash value.


回答1:


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. A unit test is just that, only the unit of code being tested. There isn't a Grails application surrounding the tested code.

I would probably add a given: section to the current test, and initialize the entities in the AppPreferences domain class there.

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.

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()))))



来源:https://stackoverflow.com/questions/41713888/accessing-the-data-in-db-via-domain-class-in-controller-via-grails-testing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!