2012-06-20 7 views
2

я есть класс домена, какGrails TestCase терпит неудачу

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)})" 
    } 

} 

и написал тестовый случай метод ToString внутри HotelStayTests

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 (среда по воскресенье)'' –

Смежные вопросы