这是我的grails服务类方法:
class SomeService {
def authenticateService
def getUserName() {
def cid = authenticateService.userDomain().customerid
def uid = authenticateService.userDomain().userid
def userDetails = User.findByCustomerIdAndUserId(cid, uid)
return userDetails.lastName+", "+userDetails.firstName
}
如何为acegi安全插件提供的authenticateService创建一个模拟单元测试?
我尝试过单元测试:
class SomeServiceTests extends GrailsUnitTestCase {
...
...
def testGetUserName() {
def service = new SomeService()
def authService = mockFor(AuthenticateService)
authService.demand.userDomain().customerid { -> return 111}
authService.demand.userDomain().userid { -> return 222}
service.authenticateService = authService.createMock()
def uName = service.getUserName()
asserNotNull uName
}
但,
当我运行测试时,它给出:
Testsuite:SomeServiceTests
测试运行:1,失败:0,错误:1,经过的时间:1.069秒
造成错误 0
java.lang.ArrayIndexOutOfBoundsException:0
at grails.test.DemandProxy.invokeMethod(GrailsMock.groovy:171)
在SomeServiceTests.testGetUserName(SomeServiceTests.groovy:230)
我无法解决问题。感谢任何帮助,谢谢...
答案 0 :(得分:2)
这些线对我来说没有意义:
authService.demand.userDomain().customerid { -> return 111}
authService.demand.userDomain().userid { -> return 222}
尝试这样的事情:
authService.demand.getUserDomain(2..2) { -> return [customerid:111, userid: 222] }