测试控制器中 MessageSource 的实例(调用调用 MessageSource 的服务。)
Instance of MessageSource in a test controller (calling a service that calls a MessageSource.)
我正在使用 grails 2.3.6,我正在尝试测试调用服务的控制器,该服务调用 MessageSource。我已经尝试了此 post (How to unit or integration test use of injected messageSource for i18n in Grails 2.0 service) 中的所有解决方案,但我仍然收到此错误:
org.springframework.context.NoSuchMessageException: No message found under code 'enrolment.state.approved' for locale 'es_ES'.
at es.portel.accessRequest.enyca.EnrolmentClientService.enrolmentTranslateState(EnrolmentClientService.groovy:30)
at es.portel.accessRequest.enyca.EnrolmentClientService.enromentsTranslateState_closure1(EnrolmentClientService.groovy:13)
at es.portel.accessRequest.enyca.EnrolmentClientService.enromentsTranslateState(EnrolmentClientService.groovy:12)
at es.portel.accessRequest.enyca.EnrolmentClientController.getAllEnrolments(EnrolmentClientController.groovy:41)
at es.portel.accessRequest.enyca.EnrolmentClientControllerSpec.test getAllEnrolments method(EnrolmentClientControllerSpec.groovy:34)
这是我的测试代码:
@TestFor(EnrolmentClientController)
@Mock(EnrolmentClientService)
class EnrolmentClientControllerSpec extends Specification{
void "test getAllEnrolments method"() {
given:
MessageSource messageSource = getMessageSource()
controller.enrolmentClientService = new EnrolmentClientService()
controller.enrolmentClientService.messageSource = messageSource
Locale locale1 = new Locale("es_ES")
messageSource.addMessage('enrolment.state.approved',locale1, 'Aprobado')
when:
controller.getAllEnrolments()
then:
assertEquals response.status, HttpServletResponse.SC_OK
assertEquals response.format, 'json'
}
}
有什么想法吗?
提前致谢。
头痛了好几次,终于解决了。我按照 saw303 告诉我的那样取消了模拟服务并进行了适当的设置。这是工作代码:
@TestFor(EnrolmentClientController)
class EnrolmentClientControllerSpec extends Specification {
@Shared
def messageSource = new StaticMessageSource()
def setupSpec() {
Locale locale1 = new Locale("es_ES")
messageSource.useCodeAsDefaultMessage = true
messageSource.addMessage "enrolment.state.approved", locale1, "Aprobado"
}
void "test getAllEnrolments method"() {
given:
controller.enrolmentClientService = new EnrolmentClientService()
controller.enrolmentClientService.messageSource = messageSource
when:
request.method = 'GET'
response.format = 'json'
controller.getAllEnrolments()
}
}
我正在使用 grails 2.3.6,我正在尝试测试调用服务的控制器,该服务调用 MessageSource。我已经尝试了此 post (How to unit or integration test use of injected messageSource for i18n in Grails 2.0 service) 中的所有解决方案,但我仍然收到此错误:
org.springframework.context.NoSuchMessageException: No message found under code 'enrolment.state.approved' for locale 'es_ES'.
at es.portel.accessRequest.enyca.EnrolmentClientService.enrolmentTranslateState(EnrolmentClientService.groovy:30)
at es.portel.accessRequest.enyca.EnrolmentClientService.enromentsTranslateState_closure1(EnrolmentClientService.groovy:13)
at es.portel.accessRequest.enyca.EnrolmentClientService.enromentsTranslateState(EnrolmentClientService.groovy:12)
at es.portel.accessRequest.enyca.EnrolmentClientController.getAllEnrolments(EnrolmentClientController.groovy:41)
at es.portel.accessRequest.enyca.EnrolmentClientControllerSpec.test getAllEnrolments method(EnrolmentClientControllerSpec.groovy:34)
这是我的测试代码:
@TestFor(EnrolmentClientController)
@Mock(EnrolmentClientService)
class EnrolmentClientControllerSpec extends Specification{
void "test getAllEnrolments method"() {
given:
MessageSource messageSource = getMessageSource()
controller.enrolmentClientService = new EnrolmentClientService()
controller.enrolmentClientService.messageSource = messageSource
Locale locale1 = new Locale("es_ES")
messageSource.addMessage('enrolment.state.approved',locale1, 'Aprobado')
when:
controller.getAllEnrolments()
then:
assertEquals response.status, HttpServletResponse.SC_OK
assertEquals response.format, 'json'
}
}
有什么想法吗? 提前致谢。
头痛了好几次,终于解决了。我按照 saw303 告诉我的那样取消了模拟服务并进行了适当的设置。这是工作代码:
@TestFor(EnrolmentClientController)
class EnrolmentClientControllerSpec extends Specification {
@Shared
def messageSource = new StaticMessageSource()
def setupSpec() {
Locale locale1 = new Locale("es_ES")
messageSource.useCodeAsDefaultMessage = true
messageSource.addMessage "enrolment.state.approved", locale1, "Aprobado"
}
void "test getAllEnrolments method"() {
given:
controller.enrolmentClientService = new EnrolmentClientService()
controller.enrolmentClientService.messageSource = messageSource
when:
request.method = 'GET'
response.format = 'json'
controller.getAllEnrolments()
}
}