[英]Service is not getting injected into Grails domain class , Grails 2.0.0 and 2.0.3
[英]Grails 2.0.0 mock service always returns true?
我正在嘗試模擬的控制器中有一個服務,但它始終返回true。 該服務如下所示:
def someService (x, y){...}
然后我在控制器測試中對其進行了嘲笑: mockservice.demand.someService () {-> return false }
它不斷返回true。 我不知道怎么了 我嘗試包括這些參數,但它仍未返回false。 這個怎么做?
PS:原諒錯別字,我現在正在打電話。 謝謝
我假設您正在控制器中使用此服務,並且單元測試來自控制器。 我還假設您閱讀了文檔中的模擬協作者 。
@TestFor(MyController)
class MyControllerSpec {
def setup() {
def mockServiceControl = mockFor(SomeService)
//here you limit the amount of times that the method should be called.
mockServiceControl.demand.someService(0..1) { x, y ->
return false
}
//probably you're missing to assign the attribute in the controller
controller.someService = mockServiceControl.createMock()
}
void testSomething() {
controller.action()
//assert response
//Then verify that the demand mocking is correct
mockServiceControl.verify()
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.