2012-06-20 91 views
2

我有一個域類像Grails的測試用例失敗

package trip.side 
import java.text.SimpleDateFormat 

class HotelStay { 
    String hotel 
    Date checkIn 
    Date checkOut 

    static constraints = { 
    } 
    String toString(){ 
    def sdf = new SimpleDateFormat("EEEE") 
    "${hotel}(${sdf.format(checkIn)} to ${sdf.format(checkOut)})" 
    } 

} 

,並寫了內HotelStayTests測試用例toString方法

void testToString() { 
     def h = new HotelStay(hotel:"Hilton") 
     def df = new SimpleDateFormat("MM/dd/yyyy") 
     h.checkIn = df.parse("10/1/2008") 
     h.checkOut = df.parse("10/5/2008") 
     println h 
     assertToString h, "Hilton (Wednesday to Sunday)" 
    } 

完全HotelStayTests類

package trip.side 



import grails.test.mixin.* 
import org.junit.* 
import java.text.SimpleDateFormat 

/** 
* See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions 
*/ 
@TestFor(HotelStay) 
class HotelStayTests { 

    void testSomething() { 
    // Simple test by creating new object and asserting it 
      // fail "Implement me" 
     HotelStay hs = new HotelStay(hotel:"Ibis") 
     assertEquals "Ibis", hs.hotel 
    } 

    void testToString() { 
     def h = new HotelStay(hotel:"Hilton") 
     def df = new SimpleDateFormat("MM/dd/yyyy") 
     h.checkIn = df.parse("10/1/2008") 
     h.checkOut = df.parse("10/5/2008") 
     println h 
     assertToString h, "Hilton (Wednesday to Sunday)" 
    } 
} 

但失敗並放棄錯誤報告

No signature of method: trip.side.HotelStayTests.assertToString() is applicable for argument types: (trip.side.HotelStay, java.lang.String) values: [Hilton(Wednesday to Sunday), Hilton (Wednesday to Sunday)] Possible solutions: testToString() 
groovy.lang.MissingMethodException: No signature of method: trip.side.HotelStayTests.assertToString() is applicable for argument types: (trip.side.HotelStay, java.lang.String) values: [Hilton(Wednesday to Sunday), Hilton (Wednesday to Sunday)] 
Possible solutions: testToString() 
    at trip.side.HotelStayTests.testToString(HotelStayTests.groovy:28) 
System output 
Hilton(Wednesday to Sunday) 

任何想法這裏怎麼了?

+1

請問你的測試類擴展'GroovyTestCase'? –

+0

@tim_yates:no ..我編輯了我的問題 – Hussy

+1

'assertToString'是'GroovyTestCase'的一部分...你有沒有試過把'extends GroovyTestCase'放到你的類定義中? –

回答

3

the GroovyTestCase class的一部分。

您的測試類需要extend GroovyTestCase獲得這一功能

+0

如果我不擴展GroovyTestCase(如在grails 2.0中)並且想要實現相同的功能?我的意思是什麼是替代assertToString? – Hussy

+1

你可以這樣做:'assert h?.toString()=='Hilton(週三至週日)'' –