Confusing behaviour with expectation

31 views
Skip to first unread message

raks

unread,
May 8, 2013, 4:44:50 AM5/8/13
to spockfr...@googlegroups.com
Hi,

I'm testing the parameter passed to a mocked method but I'm seeing strange results.

My production class is Java, here's a snippet:

void process(List<String[]> rows) {

        List<String[]> errors = new ArrayList<String[]>();

        // business logic
 
        customerRecordErrorService.write(errors);
}


in my Spock test, I want to verify the contents of errors. The first case is no errors so I'm expecting a List<String[]> with no elements.

1 * customerRecordErrorService.write(_) >> { errors ->
            assert errors.size() == 0
}

This fails with:

Condition not satisfied:

errors.size() == 0
|      |      |
[[]]   1      false

From what I can tell I'm getting a List with one element and that element is the List<String[]> I need. Why is this?

Thanks

Rakesh

Peter Niederwieser

unread,
May 8, 2013, 4:58:34 AM5/8/13
to spockfr...@googlegroups.com
As explained in the docs (1),.Groovy will match a single untyped closure parameter to the whole argument list. You can prevent this by giving the parameter a type.

PS: A more idiomatic way to write this is:

1 * customerRecordErrorService.write({ it.empty })

Cheers,
Peter


--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spockframewor...@googlegroups.com.
To post to this group, send email to spockfr...@googlegroups.com.
Visit this group at http://groups.google.com/group/spockframework?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

rakesh mailgroups

unread,
May 8, 2013, 5:12:53 AM5/8/13
to spockfr...@googlegroups.com
thanks Peter.
Reply all
Reply to author
Forward
0 new messages