Invocation not being counted

1,015 views
Skip to first unread message

Fez

unread,
Feb 18, 2011, 7:30:30 AM2/18/11
to Spock Framework - User
Hi I have the following test which fails with

Too few invocations for:

1 * userService.isRegisteredForSite(domain,site) >> null (0
invocations)

=============================
def setup() {
email = 'tes...@mailinator.com'
authid = 'gagdjwegryjh378'

user = new User(email: email, username: 'testsdh')
mockDomain User, [user]

userSession = new UserSession(authid: authid, user: user)
mockDomain UserSession, [userSession]

sessionService = Mock(SessionService.class)
controller.sessionService = sessionService
}

......

def "show user has active persistent session"() {
given:
userSession.isPersistent = true
mockParams.id = authid

when:
controller.show()

then:
1 * sessionService.hasSessionExpired(userSession, false) >>
false
}

==============================

Code snippet of code being tested :

def host = request.getHeader('Host')
def site = params.site
def userSite = userService.isRegisteredForSite(host, site)

==============================


I cannot work out why this is failing I've tried setting the
expectation to:

1 * sessionService.hasSessionExpired(_,_) >> null

and this still fails.

Can anyone see what I'm doing wrong?

Fez

unread,
Feb 18, 2011, 7:36:32 AM2/18/11
to Spock Framework - User
Actually the test I posted in the original message is the wrong one.
See below:

=============================================


def setup() {
email = 'tes...@mailinator.com'
authid = 'gagdjwegryjh378'
password = 'testpassword'

user = new User(email: email, username: 'testsdh', password:
password)
mockDomain User, [user]

userSession = new UserSession(authid: authid, user: user)
mockDomain UserSession, [userSession]

sessionService = Mock(SessionService.class)
controller.sessionService = sessionService

userService = Mock(UserService.class)
controller.userService = userService
}

....

def "user not registered for site"() {
given:
String site = "molSite"
String domain = "mailonline.com"
mockParams.site = site
mockParams.email = email
mockParams.password = password
controller.request.addHeader 'Host',domain

when:
controller.save()

then:
1 * userService.isRegisteredForSite(domain,site) >> null
}
=====================================

Fez

Peter Niederwieser

unread,
Feb 18, 2011, 7:52:57 AM2/18/11
to spockfr...@googlegroups.com
Still don't get it. First you say you tried:

1 * userService.isRegisteredForSite(domain,site) >> null

Then you say you also tried:

> 1 * sessionService.hasSessionExpired(_,_) >> null

Maybe you are just confusing things somewhere?

Is it important for your spec to verify that these methods get called, or do you just want the methods to return null? In the former case, you can remove the ">> null" part (which is the default). In the latter case, you can remove the whole interaction(s). Mocks will automatically return null/0/false for method calls they don't expect.

Cheers,
Peter

Fez

unread,
Feb 18, 2011, 9:19:32 AM2/18/11
to Spock Framework - User
Hi Peter,

In the initial post I pasted in the wrong test code.

So I am only concerned with ensuring the following :
1 * userService.isRegisteredForSite(domain,site) >> null

I want to know that this is called correctly and the params are as
expected. From what I can see this expectation should be fine.

But I get 0 invocations.

Peter Niederwieser

unread,
Feb 18, 2011, 11:15:51 AM2/18/11
to spockfr...@googlegroups.com
On 18.02.2011, at 15:19, Fez wrote:

> So I am only concerned with ensuring the following :
> 1 * userService.isRegisteredForSite(domain,site) >> null
>
> I want to know that this is called correctly and the params are as
> expected. From what I can see this expectation should be fine.
>
> But I get 0 invocations.

Probably the expectation is wrong then. To see if some other invocation on userService was made, try:

0 * userService._

If you prefer strict validation, you can add this at the end of a then-block:

0 * _ // no other interactions allowed

Cheers,
Peter

Fez

unread,
Feb 18, 2011, 11:56:27 AM2/18/11
to Spock Framework - User
I'm sorry I was being a real dim wit! The service method takes 3
params therefore :

1 * userService._ // works
and so does
1 * userService.isRegisteredForSite(user,domain,site) >> null //
works

Thanks for your help!
Reply all
Reply to author
Forward
0 new messages