ioBroker.javascript icon indicating copy to clipboard operation
ioBroker.javascript copied to clipboard

Decouple function testing from adapter execution

Open AlCalzone opened this issue 7 years ago • 4 comments

Disclaimer: This is not a high-priority thing. I'm willing to tackle this when I got the time. Just leaving it here so I don't forget.

Currently, testing the adapter functions is very time-intensive as we are always starting an instance of the adapter and run an actual script like in a production environment.

By mocking the sandbox on the testing environment, we could test the adapter functions like getState, $ and so on without actually running the adapter. This means we don't need to use the js-controller and objects/states DB for testing the script functions and we can test much faster and more specific. This also means we don't test the js-controller and its DBs over and over and limit the functional adapter tests to just the adapter. Of course, the usual tests to see if the adapter starts should stay in place.

By intercepting the calls from sandbox.getState to adapter.getState (and other functions) within the tests, we can return just the objects we want each test to receive and easily test specific behavior of each script function.

I've done this before, e.g. in https://github.com/AlCalzone/node-tradfri-client/blob/master/src/tradfri-client.test.ts where the interface to other node libs is replaced within the tests. The tests just make sure the other node libs are called with the correct arguments.

AlCalzone avatar Jun 29 '18 10:06 AlCalzone

Very good idea. Keywords: sinon, proxyrequire

пт, 29 июн. 2018 г., 12:50 AlCalzone [email protected]:

Disclaimer: This is not a high-priority thing. I'm willing to tackle this when I got the time. Just leaving it here so I don't forget.

Currently, testing the adapter functions is very time-intensive as we are always starting an instance of the adapter and run an actual script like in a production environment.

By mocking the sandbox on the testing environment, we could test the adapter functions like getState, $ and so on without actually running the adapter. This means we don't need to use the js-controller and objects/states DB for testing the script functions and we can test much faster and more specific. This also means we don't test the js-controller and its DBs over and over and limit the functional adapter tests to just the adapter. Of course, the usual tests to see if the adapter starts should stay in place.

By intercepting the calls from sandbox.getState to adapter.getState (and other functions) within the tests, we can return just the objects we want each test to receive and easily test specific behavior of each script function.

I've done this before, e.g. in https://github.com/AlCalzone/node-tradfri-client/blob/master/src/tradfri-client.test.ts where the interface to other node libs is replaced within the tests. The tests just make sure the other node libs are called with the correct arguments.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ioBroker/ioBroker.javascript/issues/145, or mute the thread https://github.com/notifications/unsubscribe-auth/AEXqgHyyGG81eS3eQ8HaFAYNfYd_UrWKks5uBgaEgaJpZM4U8079 .

GermanBluefox avatar Jun 29 '18 11:06 GermanBluefox

Keywords: sinon, proxyrequire

exactly :)

AlCalzone avatar Jun 29 '18 11:06 AlCalzone

@AlCalzone I think this is partially done

GermanBluefox avatar Feb 26 '19 21:02 GermanBluefox

The framework is now there with @iobroker/testing, but we need to actually use it :) So I'd leave this open for now

AlCalzone avatar Feb 27 '19 06:02 AlCalzone