jeudi 26 février 2015

How do I test that a method was called within a grails asynchronous promise?

I'm trying the verify that a method contained in a promise was called, using a unit test.


I have a CallerService, that will call classA.methodA() within a task,



class CallerService {

def classA

def callA() {
Promise p = task {
classA.methodA()invocation)
}
p.onComplete {
println "complete"
}
p.onError { Throwable err ->
println "there was an error"
}
}
}


and a unit test that mocks ClassA and tries the verify that methodA was called once.



def "calling A"() {
given:"a mock classA"
def mockClassA = Mock(ClassA)
service.classA = mockClassA

when:"callA is called"
service.callA()

then:"methodA should be called once"
1* mockClassA.methodA()
}


The tests fails because the mock was called twice.



| Failure: calling A(promisespike.CallerServiceSpec)
| Too many invocations for:
1* mockClassA.methodA() (2 invocations)
Matching invocations (ordered by last occurrence):
2 * mockClassA.methodA() <-- this triggered the error


Is this the result I should be expecting or have I setup my test incorrectly?


Aucun commentaire:

Enregistrer un commentaire