我正在努力通過'Grails in Action',我在嘗試編寫Integration時遇到問題測試我的一項服務。在Grails中的測試服務產生'org.junit.ComparisonFailure:期望:<An[a]nymous>但是:<An[o]nymous>'錯誤
我意識到我在使用Grails 2.0.3,而本書是在Grails 1.x.x的基礎上編寫的。
這裏是我的服務:
package qotd
類QuoteService {
boolean transactional = true
def getRandomQuote(){
def allQuotes = Quote.list()
def randomQuote
if(allQuotes.size() > 0){
def randomIndex = new Random().nextInt(allQuotes.size())
randomQuote = allQuotes[randomIndex]
}
else{
randomQuote = getStaticQuote()
}
return randomQuote
}
def getStaticQuote(){
return new Quote(author: "Anonymous",
content: "Real Programmers Don't eat quiche")
}
}
而下面是我的集成測試,位於 '/測試/集成/ QOTD /'
package qotd
進口靜態org.junit.Assert。*
進口org.junit。*
類QuoteServiceIntegrationTests擴展的GroovyTestCase {
def quoteService
@Before
void setUp() {
}
@After
void tearDown() {
}
@Test
void testStaticQuote() {
def staticQuote = quoteService.getStaticQuote()
assertNotNull quoteService
assertEquals "Ananymous",staticQuote.author
assertEquals "Real Programmers Don't Eat Quiche",staticQuote.content
}
}
以防萬一這可能是相關的,在這裏是我正在測試以上內容的報價類:
包qotd
類報價{
String content
String author
Date created = new Date()
static constraints = {
author(blank:false)
content(maxSize:1000,blank:false)
}
}
當我運行我的測試,使用 '測試應用程序 - 整合' 我得到以下幾點:
運行1個集成測試... 1的1
失敗:testStaticQuote(qotd.QuoteServiceIntegrationTests)
org.junit.ComparisonFailure:expected:An [a] nymous but was:[o] nymous
在org.junit.Assert.assertEquals(Assert.java:125)
在org.junit.Assert.assertEquals(Assert.java:147)
在qotd.QuoteServiceIntegrationTests.testStaticQuote(QuoteServiceIntegrationTests.groovy:24)
任何洞察力將不勝感激。謝謝你們!
你是完全正確的,在這裏,我想,這是一個錯誤!謝謝 :) –