测试弹簧安全保护页面

时间:2014-05-08 09:35:11

标签: grails spock geb

我正在尝试使用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)

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

2 个答案:

答案 0 :(得分:2)

您可以使用via(),这是针对涉及重定向的情况而引入的,它会像to()那样转到页面网址,但是在检查"时它没有做到#34;。所以你的测试看起来像:

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

    then:
    at AuthPage
}

答案 1 :(得分:0)

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

相关问题