jeudi 2 juillet 2020

Too few invocations 0 when checking cardinality with Spock

I have a simpler controller method that does the following:

def updateName(){
    def idUser = params.id
    User changeUser = User.get(idUser)
    changeUser.name = "newname"
    changeUser.save(flush: true)
}

And the following Spock test:

def "updateName should edit user and save it"(){

given: "the id of the user"
    User currentUser = new User([name: "hector" , age: 12]).save(flush: true)
    params.id = User.last().id

when: "the updateName method is called"
    controller.updateName()

then: "name should have change it and save has to be called just one time"
    assert currentUser.name == "newname" // Success
    1 * _.save() // Too few invocations, 0.


}

I have seen all the related questions in SO about this topic, and people suggest using Mocks and Spy, but I don't really see why should I be using them and anyway I didn't get them to work for me, I tried to create a User mock and change my cardinality assert to:

1 * userMock.save()

But it didn't work for me... can I have some help with this?

Aucun commentaire:

Enregistrer un commentaire