【问题标题】:Testing spring security protected pages测试弹簧安全保护页面
【发布时间】:2014-05-08 09:35:11
【问题描述】:

我正在尝试使用 Geb 测试我的主页是否受到 Spring Security 的保护。

这是我的测试:

void "Unauthenticated user is redirected to login page."() {
given:
    to HomePage
    via AuthPage

expect:
    at AuthPage

}

当我尝试运行这个测试时,geb 说我的断言失败了。它说我在我的 AuthPage 中,而不是在我的主页中。

这是堆栈跟踪:

| Failure:  Unauthenticated user is redirected to login page.(home.HomePageSpec)
|  Assertion failed: 
title ==~ /Home/
|     |
Login false
at pages.home.HomePage._clinit__closure1(HomePage.groovy:8)
at geb.Page.verifyThisPageAtOnly(Page.groovy:165)
at geb.Page.verifyAt(Page.groovy:133)
at geb.Browser.doAt(Browser.groovy:358)
at geb.Browser.at(Browser.groovy:289)
at geb.Browser.to(Browser.groovy:436)
at geb.Browser.to(Browser.groovy:412)
at geb.spock.GebSpec.methodMissing(GebSpec.groovy:51)
at home.HomePageSpec.Unauthenticated user is redirected to login page.(HomePageSpec.groovy:22)

但这是我所期待的行为!为什么会失败?

【问题讨论】:

    标签: grails spock geb


    【解决方案1】:

    您可以使用via(),它是在涉及重定向的情况下引入的,它会像to() 一样转到页面网址,但它不会“检查”。所以你的测试看起来像:

    void "Unauthenticated user is redirected to login page."() {
        when: 
        via HomePage
    
        then:
        at AuthPage
    }
    

    【讨论】:

      【解决方案2】:

      geb 的to() 方法隐式调用静态at closure。这就是我的测试失败的原因。当我打电话给HomePage 时,它会检查我是否到达了HomePage,我从来没有这样做过,因为它被重定向了。我改用go <HOME_PAGE_URL>

      【讨论】:

      • 您可以使用via HomePage(转到主页网址并且不进行at 检查)和at AuthPage 来改进您的代码。这样您就不必在使用 go() 时将页面 url 放入测试中
      • 这似乎对我不起作用。通过 HomePage 使用时,我仍然会转到 AuthPage。
      • 是的,因为您被您的应用程序重定向。
      • 它不起作用。它说我的断言失败。 go 是唯一可行的方法。
      猜你喜欢
      • 2016-03-28
      • 2019-02-03
      • 2023-01-03
      • 2018-09-08
      • 1970-01-01
      • 2019-03-03
      • 1970-01-01
      • 1970-01-01
      • 2014-02-17
      相关资源
      最近更新 更多