3
我最近將Grails 1.3.7項目升級到了Grails 2.0.4,並注意到我的許多單元測試嘲笑已經開始失敗。控制器測試看起來傳遞得很好,問題出現在你有服務彼此協作並試圖模擬對協作者的調用時。關於它的奇怪的部分是,如果我運行一個測試,它通過,但只要我運行整個套件,他們無法給錯誤:Grails 2.0錯誤中的模擬測試
No more calls to 'getName' expected at this point. End of demands.
junit.framework.AssertionFailedError: No more calls to 'getName' expected at this point. End of demands.
我一直在使用GMock而不是新的MockFor甚至試圖(),但得到這個非常類似的錯誤:
No more calls to 'getSimpleName' expected at this point. End of demands.
junit.framework.AssertionFailedError: No more calls to 'getSimpleName' expected at this point. End of demands.
這裏是展示如何複製,我發現了錯誤,並在https://github.com/punkisdead/FunWithMocks在GitHub上的整個樣本項目一個人爲的例子。任何想法如何使這項工作?
BarController:
package funwithmocks
class BarController {
def barService
def fooService
def index() { }
}
BarService:
package funwithmocks
class BarService {
def fooService
def bazService
def serviceMethod() {
}
}
BarControllerTests:
package funwithmocks
import grails.test.mixin.*
import org.junit.*
import groovy.mock.interceptor.MockFor
/**
* See the API for {@link grails.test.mixin.web.ControllerUnitTestMixin} for usage instructions
*/
@TestFor(BarController)
class BarControllerTests {
def fooService
def barService
@Before
public void setUp() {
fooService = new MockFor(FooService)
fooService.use {
controller.fooService = new FooService()
}
barService = new MockFor(BarService)
barService.use {
controller.barService = new BarService()
}
}
@Test
void doSomething() {
controller.index()
}
}
BarServiceTests: 包funwithmocks
import grails.test.mixin.*
import org.junit.*
import groovy.mock.interceptor.MockFor
/**
* See the API for {@link grails.test.mixin.services.ServiceUnitTestMixin} for usage instructions
*/
@TestFor(BarService)
class BarServiceTests {
def fooService
def bazService
@Before
public void setUp() {
fooService = new MockFor(FooService)
fooService.use {
service.fooService = new FooService()
}
bazService = new MockFor(BazService)
bazService.use {
service.bazService = new BazService()
}
}
@Test
void callSomeService() {
service.serviceMethod()
}
}
謝謝。我不確定他們爲什麼不在文檔中做得更清楚一些,但是這確實有用。似乎很奇怪它會像那樣突破。 –