Too few interactions in a Spock test for a Grails service - testing

I thought I've understood Spock interactions but I have to admin that I'm still missing some pieces of the picture.
Alright, here my problem: I have a method in a Grails service which performs some operations, including a call to a void method of the same service class. Here's the code:
class myService {
void myStuff(def myObj, params) {
// do my stuff
anotherMethod(myObj)
//do my stuff again
}
void anotherMethod(myObj) {
// do other stuff
}
}
I want to be sure that myStuff method calls anotherMethod, to test and document the correct behaviour.
So, here's the test in my Spock Specification class:
void 'test myStuff method'() {
given: 'my object and some params'
// doesn't really matter what I'm doing here
MyObject myObj = new MyObject()
params = [title: 'my object']
when: 'we call myStuff()'
service.myStuff(myObj, params)
then: 'anotherMethod() is called exactly one times'
1 * service.anotherMethod(myObj)
}
}
The error I get is:
Too few invocations for:
1 * service.anotherMethod(myObj) (0 invocations)
What do you think? What's wrong?
As always, thanks in advance.

You are asking for a very special, and generally discouraged, form of mock called partial mocking where methods on the class under test are mocked. Spock supports this since 0.7, but you'll have to create a Spy() rather than a Mock(). Also note that you cannot mock private methods. For more information on spies, see the reference documentation.

Related

Spock: How to properly share features between implementations of Specification

I encountered an unfortunate aspect of Spock Framework (1.3-groovy-2.5), which I use for integration testing of a Gradle plugin.
The code sample
Parent class:
class ClassA extends Specification {
def setupSpec() {
System.out.println("In ClassA setupSpec()")
}
def "base feature"() {
expect:
true
}
}
Child class:
class ClassB extends ClassA {
#Override
def setupSpec() {
System.out.println("In ClassB setupSpec()")
}
def "extended feature"() {
expect:
true
}
}
When I run tests in ClassB, both versions of setupSpec() are called:
In ClassA setupSpec()
In ClassB setupSpec()
Of course, if I call method via native Groovy ways:
class Main {
static void main(String[] args) {
ClassB classB = new ClassB()
classB.setupSpec()
}
}
Then I see only what is expected:
In ClassB setupSpec()
So, evidently, this is a Spock feature of some kind.
Question
In practice, what is the suggested way of inheriting from implementations of Specification while overriding setup logic?
As documented in Fixture Method Invocation Order
If fixture methods are overridden in a specification subclass then
setup() of the superclass will run before setup() of the subclass.
cleanup() works in reverse order, that is cleanup() of the subclass
will execute before cleanup() of the superclass. setupSpec() and
cleanupSpec() behave in the same way. There is no need to explicitly
call super.setup() or super.cleanup() as Spock will automatically find
and execute fixture methods at all levels in an inheritance hierarchy.
The easiest way would be to just move the logic to another method that you can override.
class Base extends Specification {
def setupSpec() {
init()
}
def init() {
println "foo"
}
def "let's try this!"() {
expect:
Math.max(1, 2) == 2
}
}
class Sub extends Base {
#Override
def init() {
println "bar"
}
}
As documented, fixture methods are not meant to override each other but to complement each other, i.e. they are all called in a specific, logical order. Like you said, this is a feature.
Hence, my answer is: There is no suggested way to override setup logic. Instead, the suggested way is to design your base and derived specifications in such a way that overriding is not necessary. I never had any problems to do so and hope you shall solve your problem easily too.
Your sample code is too schematic to say anything more, but basically think about fixture methods in base classes as responsible for to setting up and cleaning up fixture fields in there, while derived specs' fixture methods would take care of additional fixtures specific to those classes. In your example there also is a feature method in the base spec, which I find rather strange because it would be executed for the base spec itself and each time a derived spec is executed. I rather like to think of base specs as practically abstract (avoiding to add any feature methods to them and them being picked up by the test runner), but I am sure there are cases in which what you sketched above might also be helpful. I just cannot think of one now.

How to mock a constructor with a spy?

I am currently trying to test my Exposed Kotlin code. I have a table that follows the form
object Foo: Table() {
*parameters*
}
and a method that looks something like
fun addNewFoo(){
Foo.insert { ... }
}
I'm testing addNewFoo and I want to verify the insert occurred, ideally using something like
verify { FooSpy.insert { ... } }
How do I mock the Foo table to be a spy so I can verify the call occurred, or what other approach should I take to verify this method being called?
You can first mock your singleton Foo class using mockkObject() and then verify. Here is the code:
mockkObject(Foo) // mock the object
addNewFoo() // call function that we're testing
verify { Foo.insert(any()) } // verify
There is discussion of ways to go about it: https://github.com/JetBrains/Exposed/issues/317
There seems to be no real intended way for testing but making small test tables in a test data base is the closest you can get.

Testing private methods in Raku

Is there a way to test private methods in Raku?
I understand that one should ideally define their tests targeting the public methods, but is there a way to do it "the wrong way"? :)
I initially thought about defining a subclass for the Testing that inherited from the class I wanted to test and do the tests there, but it seems that private methods are not inherited.
Then I saw the 'trusts' routine, but I wouldn't want to reference a Testing class on any of the classes of the code.
Is there something like changing the 'private' property of a method via introspection?
What would be the best way to call/test a private method?
This can be done using introspection.
Consider this is the class you want to test:
class SomeClass {
has Int $!attribute;
method set-value(Int $value) returns Nil {
$!attribute = $value;
return;
}
method get-value returns Int {
return $!attribute;
}
# Private method
method !increase-value-by(Int $extra) returns Nil {
$!attribute += $extra;
return;
}
}
You may create a test like this:
use Test;
use SomeClass;
plan 3;
my SomeClass $some-class = SomeClass.new;
my Method:D $increase-value = $some-class.^find_private_method: 'increase-value-by';
$some-class.set-value: 1;
$increase-value($some-class, 4);
is $some-class.get-value, 5, '1+4 = 5';
$increase-value($some-class, 5);
is $some-class.get-value, 10, '5+5 = 10';
my SomeClass $a-new-class = SomeClass.new;
$a-new-class.set-value: 0;
$increase-value($a-new-class, -1);
is $a-new-class.get-value, -1, '0+(-1) = -1; The method can be used on a new class';
done-testing;
You first create an instance of the class and the use ^find_private_method to get its private Method. Then you can call that Method by passing an instance of a class as the first parameter.
There's a more complete explanation on this answer:
How do you access private methods or attributes from outside the type they belong to?
A fresh cup of tea and #Julio's and #JJ's answers inspired the following:
class SomeClass { method !private ($foo) { say $foo } }
use MONKEY-TYPING; augment class SomeClass { trusts GLOBAL }
my SomeClass $some-class = SomeClass.new;
$some-class!SomeClass::private(42); # 42
My solution tweaks the class using monkey typing. Monkey typing is a generally dodgy thing to do (hence the LOUD pragma). But it seems tailor made for a case just like this. Augment the class with a trusts GLOBAL and Bob's your Uncle.
Raku requires the SomeClass:: qualification for this to work. (Perhaps when RakuAST macros arrive there'll be a tidy way to get around that.) My inclination is to think that having to write a class qualification is OK, and the above solution is much better than the following, but YMMV...
Perhaps, instead:
use MONKEY-TYPING;
augment class SomeClass {
multi method FALLBACK ($name where .starts-with('!!!'), |args) {
.(self, |args) with $?CLASS.^find_private_method: $name.substr: 3
}
}
and then:
$some-class.'!!!private'(42); # 42
I've used:
A multi for the FALLBACK, and have required that the method name string starts with !!!;
A regular method call (. not !);
Calling the method by a string version of its name.
The multi and !!! is in case the class being tested already has one or more FALLBACK methods declared.
A convention of prepending !!! seems more or less guaranteed to ensure that the testing code will never interfere with how the class is supposed to work. (In particular, if there were some call to a private method that didn't exist, and there was existing FALLBACK handling, it would handle that case without this monkey FALLBACK getting involved.)
It should also alert anyone reading the test code that something odd is going on, in the incredibly unlikely case that something weird did start happening, either because I'm missing something that I just can't see, or because some FALLBACK code within a class just so happened to use the same convention.
Besides using introspection, you can try and use a external helper role to access all private methods and call them directly. For instance:
role Privateer {
method test-private-method ( $method-name, |c ) {
self!"$method-name"(|c);
}
}
class Privateed does Privateer {
method !private() { return "⌣" }
}
my $obj = Privateed.new;
say $obj.test-private-method( "private" );
The key here is to call a method by name, which you can do with public and private methods, although for private methods you need to use their special syntax self!.

Specifying method's behaviour via EXPECT_CALL vs in body

From what I understand gmock (and I'm new to it) EXPECT_CALL allows for specifying how a method will behave when it's called (in this case I'm mostly interested in what it will return). But I could just as well define the method explicitly with its body. Example:
class Factory
{
int createSomething();
};
class MockFactory : public Factory
{
MOCK_METHOD0(createSomething, int());
};
int main()
{
...
int something(5);
MockFactory mockFactory;
EXPECT_CALL(mockFactory, createSomething()).WillRepeatedly(Return(something));
...
}
vs
class MockFactory : public Factory
{
int createSomething()
{
return 5;
}
};
Now, if createSomething were to behave differently (return different things) in different scenarios then obviously I should use EXPECT_CALL. But if it's going to always return the same thing wouldn't it be better to just explicitly define the method's body? (Note that other methods in the mocked class might still use EXPECT_CALL.)
When you define a method you miss all the flexibility that mocking that method can give you in the tests.
If you need to assert in a test that createSomething gets called, you can only do it if you have mocked it, not if you have a standard method definition. Not in this case, but in case of methods taking parameters, it's even better to have a mock.
If you need to set up a default action that your method should perform, even when you don't set any expectations on it, do so using ON_CALL macro in the SetUp member function of a TestFixture.

How can I write test cases as classes in dojo

I want to be able to write testcases as class hierarchies in dojo/doh.
But when the tests get called they seem to be called using hitch hence looses the inherited methods.
So far I've only been able to write test cases which are independant functions, but i'd like to refractor some of the common setup into a separate method
There might be a better way, but I just did this by declaring a BaseTest class that contains an array of test objects.
Example:
baseFieldViewTests: [{
name: "Test input widget()",
runTest: function () {
var inputWidget = this.field.getInputWidget();
doh.assertTrue(inputWidget.get, "Input widget has no get method");
doh.assertTrue(inputWidget.set, "Input widget has no set method");
doh.assertTrue(inputWidget.placeAt, "Input widget has no placeAt method");
}
},
Then in the subclass test I iterate through the base class tests and register them:
/**
* Register base field view tests
*/
baseFieldViewTests = new BaseFieldViewTest().baseFieldViewTests;
for (test in baseFieldViewTests) {
if (baseFieldViewTests[test]) {
doh.register("component/form/text/ReadOnlyTextViewTest",
{
name: baseFieldViewTests[test].name,
setUp: setUp,
runTest: baseFieldViewTests[test].runTest,
tearDown: tearDown
});
}
}
Not too elegant but it seems to work.