2013-10-09 43 views
0

鑑於這種映射:Grails的測試網址參數

"/student/degree/$studentid/$degreecode"(controller:'student', action:'degree') 

我想下面的試驗和失敗,都與AssertionFailedError: '...did not match any mapping'無一不是做功能有效的URL。我可以通過一個只是'/ student/degree'的測試網址來取得成功,我認爲這個網址會因爲參數的要求而失敗。

看來assertURL方法沒有處理多個參數。這是否僅適用於'id',並且不可能爲這些URL創建測試?

@TestFor(UrlMappings) 
@Mock([StudentController]) 
class UrlMappingsTests { 
    void testUrlMappings() { 
     assertForwardUrlMapping("/student/degree/102345678/N", controller: "student", action: "degree") { 
      assertForwardUrlMapping("/student/degree/102345678/N", controller: "student", action: "degree") { 
       studentid = 102345678 
       degreecode = "N" 
      } 
     } 
    } 
} 

回答

0

測試使用Grails 2.2.4,和它的作品對我來說,我只是添加自定義映射類:

package test 

class MyUrlMappings { 
    static mappings = { 
    "/test/show/$myId/$myVal"(controller: "test", action: "show") 
    } 
} 

import spock.lang.* 
import test.MyUrlMappings 

@TestFor(MyUrlMappings) 
@Mock([TestController]) 
//I'm using Spock 
class MyUrlMappingsSpec extends Specification { 

    void "mapping should consider myId and myVal"() { 
    expect: 
     assertForwardUrlMapping("/test/show/1/tst", controller: "test", action: "show") { 
     //all params considered as String 
     myId = '1' 
     myVal = 'tst' 
     } 
    } 

} 
+0

@dmahapatro感謝您的編輯。我在發佈之前實際測試了這個,並且不需要'extends Specification'。 –

+0

測試用例是否會理解'expect:'是不是來自'Specification'的繼承?那是我的想法,如果我錯了,請糾正我。 – dmahapatro

+0

@dmahapatro對,我現在看。它的工作原理是因爲'assertForwardUrlMapping'會引發錯誤。感謝您指出:-) –