I am trying to test a call to another object instance method. I have injected the User object into the Chitter class and stored the new object in a variable. The methods login(name) and logOut(name) calls the object's method, injected into chitter class. The Object, users, is a separate class and is tested, so the methods 'loginUser(name)' and 'logOutUser()' are working.
I have tried using the following to test that when login(name) is called, it calls the loginUser(name) once. But I am getting the following error message:
Too few invocations for:
1 * usersSignedUp.loginUser("Spike") (0 invocations)
Unmatched invocations (ordered by similarity):
None
at org.spockframework.mock.runtime.InteractionScope.verifyInteractions(InteractionScope.java:78)
at org.spockframework.mock.runtime.MockController.leaveScope(MockController.java:76)
at ChitterSpec.Chitter can login a user(ChitterSpec.groovy:24)
I get the same message for the logout() method too.
I assume I am using mocks the wrong way. But I did the same thing in another project and it worked, although I did not create the other object when I instantiated the object.
How do I get this test pass
Here is the code:
//Chitter.groovy
class Chitter {
private def usersSignedUp
def Chitter(users = new Users()){
usersSignedUp = users
}
def getActiveUser(){
usersSignedUp.getLoggedInUser()
}
def getUsersSignedUp(){
this.usersSignedUp
}
//These are the methods the tests are failing for
def login(name) {
usersSignedUp.loginUser(name)
}
def logOut() {
usersSignedUp.logOutUser()
}
}
For the tests (using spock and gradle)
//ChitterSpec.groovy
import spock.lang.*
class ChitterSpec extends Specification {
def chitter
Users users
def setup() {
users = GroovySpy()
chitter = new Chitter()
}
def 'no user has logged in at start'() {
expect:
chitter.getActiveUser() == null
}
def 'no users stored at the start'() {
expect:
chitter.getUsersSignedUp().getListofUsers() == []
}
//Failing tests below
def 'Chitter can login a user'() {
when:
chitter.login('Spike')
then:
1 * users.loginUser("Spike")
}
def 'Chitter can logout a user'() {
when:
chitter.logOut()
then:
1 * users.logOutUser()
}
}
The rest of my code can be seen here http://ift.tt/2alJtSS
Aucun commentaire:
Enregistrer un commentaire