我正在努力解决'Grails in Action'问题,当我尝试为我的某项服务编写集成测试时,我遇到了一个问题。
我意识到我使用的是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")
}
}
以下是我的集成测试,位于'/ test / integration / qotd /'
package qotd
import static org.junit.Assert。*
import 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
}
}
以防它可能是相关的,这是我测试上面内容的Quote类:
package qotd
班级引用{
String content
String author
Date created = new Date()
static constraints = {
author(blank:false)
content(maxSize:1000,blank:false)
}
}
当我运行我的测试时,使用'test-app -integration',我得到以下内容:
运行1个集成测试... 1 of 1
失败:testStaticQuote(qotd.QuoteServiceIntegrationTests)
org.junit.ComparisonFailure:expected:An [a] nousous但是:An [o] nymous
在org.junit.Assert.assertEquals(Assert.java:125)
在org.junit.Assert.assertEquals(Assert.java:147)
在qotd.QuoteServiceIntegrationTests.testStaticQuote(QuoteServiceIntegrationTests.groovy:24)
任何见解都将受到赞赏。谢谢大家!
答案 0 :(得分:3)
你在这一行错误地拼写了“匿名”
assertEquals "Ananymous",staticQuote.author