gpt4 book ai didi

unit-testing - 如何模拟acegi身份验证服务进行单元测试?

转载 作者:行者123 更新时间:2023-12-02 14:38:03 25 4
gpt4 key购买 nike

这是我的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
Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 1.069 sec
Caused an ERROR
0
java.lang.ArrayIndexOutOfBoundsException: 0
at grails.test.DemandProxy.invokeMethod(GrailsMock.groovy:171)
at SomeServiceTests.testGetUserName(SomeServiceTests.groovy:230)



我无法解决问题。任何帮助表示赞赏,谢谢...

最佳答案

这些行对我来说没有意义:

authService.demand.userDomain().customerid { -> return 111}
authService.demand.userDomain().userid { -> return 222}

尝试这样的事情:
authService.demand.getUserDomain(2..2) { -> return [customerid:111, userid: 222] }

关于unit-testing - 如何模拟acegi身份验证服务进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5717211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com