【问题标题】:Grails 2.4.4 coverage springSecurityServiceGrails 2.4.4 覆盖springSecurityService
【发布时间】:2023-03-25 17:53:02
【问题描述】:

我有 grails 2.4.4 和 Cobertura 作为隐蔽测试。我有这段代码要测试:

def viewMailTemplates(){
    User user = User.findByEmail(springSecurityService.authentication.principal)
    def token = user.activationToken
    def urlActivacion = request.getScheme() + '://' + request.getServerName() +":"+ request.getServerPort() + createLink(controller: "user", action: "activation", params: [token: token])

    def srcImage = request.getScheme() + '://' + request.getServerName() +":"+ request.getServerPort()+ "/assets/logoOlu.jpg"
    render(view: 'mailTemplates/_activationES', model: [srcImage: srcImage, urlActivacion: urlActivacion])
}

我有这个测试:

@TestMixin(GrailsUnitTestMixin)
@TestFor( UserController )
@Mock([UserController, User])

class UserControllerSpec extends Specification {

 void "Test the viewMailTemplates action returns the correct model"() {
      when: "The viewMailTemplates action is executed"
        controller.viewMailTemplates()
        then: "The model is correct"
        status == 405
        }

但问题是测试模式无法访问数据库并通过此测试,因为 springSecurityService=null

如果我执行“User.get(1)”之类的操作,结果为 null,即使我有这个用户,并且程序(正在开发中)也可以使用 Mysql 与 localhost 中的数据库正常工作

我怎样才能通过这个测试?如何为这个测试创建一个新的springSecurityService?

【问题讨论】:

    标签: grails spring-security integration-testing cobertura


    【解决方案1】:

    你可以让 springSecurityService 成为一个地图,比如:

    void "Test the viewMailTemplates action returns the correct model"() {
        given:
            controller.springSecurityService = [authentication:[principal: 'email_address_of_user_in_db']]
        when:
             controller.viewMailTemplates()
        then: "The model is correct"
            status == 405
    }
    

    【讨论】:

    • 谢谢,但用户返回 null。我认为是因为它在数据库中搜索,但确实无法连接,或者发生了无法在测试中读取数据库的情况
    • 您是否真的在设置方法或引导程序或其他地方在数据库中添加了用户?
    • 是的,我尝试从数据库中读取此用户,或在测试中创建,操作系统在测试中创建并保存(但它没有)但没有任何效果
    【解决方案2】:

    根据设计,单元测试不连接到数据库,而是模拟内存中域的存储。 (另一方面,集成测试确实连接到真实数据库。)

    对于每个单元测试,您必须“手动”保存测试所需的实例。为避免重复,您可以使用setup 方法并保存将被类中所有测试使用的实例。

    最后,您可以按照 Mike W 的建议模拟 SpringSecurityService

    @TestMixin(GrailsUnitTestMixin)
    @TestFor( UserController )
    @Mock([UserController, User])
    
    class UserControllerSpec extends Specification {
    
        def setup() {
            new User(username:'test', email:'demo@test.com').save(flush:true)
            assert User.count() == 1
    
            controller.springSecurityService = [authentication:[principal: 'demo@test.com']]
        }
    
        void "Test the viewMailTemplates action returns the correct model"() {
            when: "The viewMailTemplates action is executed"
                controller.viewMailTemplates()
            then: "The model is correct"
                status == 405
        }
    }
    

    【讨论】:

    • 谢谢,我试过了,但它给了我这个错误:Condition not satisfied: User.count() == 1 | | 0 false Expected :1 Actual :0 我想是关于可以在测试时访问数据库(集成或单元,我都试过了)。有什么建议吗?
    • 表示上一行没有保存用户。我只是以usernameemail 属性为例。您需要正确设置在特定 User 域中定义的所有属性。
    • 您还可以使用save(flush:true, failOnError:true) 查看失败的验证并阻止保存User
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多