2013-07-09 20 views
1

我正在嘗試使用Grails編寫一個簡單的Geb/Spock測試,但我收到以下測試失敗。Geb'at null'斷言頁面

| Failure: login works correctly(...UserAuthAcceptanceSpec) 
| Condition not satisfied: 

at HomePage 
| 
null 

我可以通過使用調試器使用瀏覽器按照測試可以看到,應用程序運行正常,並正在顯示正確的航向。但是,當我嘗試調用at檢查器時,測試失敗。任何人都可以告訴我爲什麼測試中的最終斷言可能會失敗,爲什麼'at'檢查器顯示爲空?

這是我的代碼:(蓋布v0.9.0,Grails的2.2.2)

斯波克規格:

class UserAuthAcceptanceSpec extends GebReportingSpec { 

    def "login works correctly"() { 

     given: "the correct credentials" 
      def theCorrectUsername = "admin" 
      def theCorrectPassword = "password" 

     when: "logging in" 
      to LoginPage 
      username = theCorrectUsername 
      password = theCorrectPassword 
      submitButton.click() //([HomePage, LoginPage]) 

     then: "the welcome page is shown" 
      heading =~ /(?i)Welcome.*/ // <- same as 'at' checker in HomePage 
     and: "the 'at' checker works" 
      at HomePage     // <- fails 

    } 

LoginPage

class LoginPage extends Page { 

    final String path = "/login/auth" 

    static content = { 
     heading(required: false, wait:true) { $("h1").text() } 
     username  { $("input", name:"j_username") } 
     password  { $("input", name:"j_password") } 
     submitButton { $("input", id:"submit") } 
    } 

    static at = { 
     title =~ /Login.*/ 
    } 

} 

主頁

class HomePage extends Page { 

    final String path = "/" 

    static content = { 
     heading(required: false, wait:true) { $("h1").text() } 
    } 

    static at = { 
     heading =~ /(?i)Welcome.*/ 
    } 

} 

回答

1

at檢查器應該使用==~而不是=~

蓋布的隱式斷言意味着語句:

heading1 =~ /(?i)Welcome.*/ 
heading2 ==~ /(?i)Welcome.*/ 

有效地成爲:

assert (heading1 =~ /(?i)Welcome.*/) == true  // [1]  
assert (heading2 ==~ /(?i)Welcome.*/) == true // [2] 

[2]將評估爲布爾值和通過/失敗如預期的,而[1]的計算結果爲導致失敗的java.util.regex.Matcher

請參閱Groovy Regex FAQ瞭解兩種語法之間的區別。