gpt4 book ai didi

unit-testing - 模拟配置以对Grails Controller 进行单元测试

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

在grails 1.3.7中,我通过 Controller 中的ConfigurationHolder访问一些外部属性。仔细搜索建议我可以在单元测试的设置中使用以下内容:

    mockConfig '''
sys.admin.username = "username"
sys.admin.password = "password"
'''

当我的单元测试运行时,ConfigruationHolder.config为空。因此,更多的Google搜索使我尝试在设置中尝试以下操作:
    def mockedConfig = new ConfigObject()
mockedConfig.sys.admin.username = "username"
mockedConfig.sys.admin.password = "password"
ConfigurationHolder.config = mockedConfig

在单元测试中仍然得到一个空的配置对象。我还尝试了这种第一种样式的语法,但也没有用:
    mockConfig('''
sys { admin {
username = 'username'
password = 'password'
} }
''')

我不熟悉Grails,所以我绊倒了这一步,并希望对我的 Controller 进行测试。如何解决空配置对象?

最佳答案

,这是一个时机问题。我在 Controller 类中拥有对configurationholder的引用:

def config = ConfigurationHolder.config

将其移到需要它的方法内部即可。

关于unit-testing - 模拟配置以对Grails Controller 进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6708333/

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