I have QML tests that work for me without compilation in any C ++ code.
In my case, I have a C ++ object controller with the left_motor property, which is another object and has a property speed.
Please note that the speed is readable but not writable. Any updates will occur through the slots. In QML, it looks like this: controller.left_motor.onGuiSpeedChanged (speed)
I was able to mock this in QML using components, properties and some javascript.
Item { // mock of controller id: controller property alias left_motor: left_motor Item { id: left_motor property int speed: 0 function onGuiSpeedChanged(arg) { speed = arg } } } property alias controller: controller
Now calls the controller .left_motor.onGuiSpeedChanged (speed), as before, but connected to the mock function. I can even read the speed property to know that a call has occurred.
Here is my test function (the code I'm testing is part of page 1):
function test_set_speed() { console.log("controller.left_motor.speed: " + controller.left_motor.speed) var got = page1.set_left_speed(250) compare(got, 250, "set_left_speed() return incorrect") console.log("controller.left_motor.speed: " + controller.left_motor.speed) }
Please note that it is important to use slots instead of writable properties. Calling a slot looks the same as calling a function, and you can mock it as such. I could not figure out a way to mock property creation.
I started trying rewritable properties because this was the first thing in the documentation for linking C ++ and QML. It incorporates QML and C ++ as expected, but cannot be worn for testing.
Dan christian
source share