qmltestcase 找不到 c++ class 方法

qmltestcase can't find c++ class method

我有一个简单的应用程序 运行s 成功;但是,我在尝试使用 qmltest 运行 测试用例时遇到困难。在 documentation 中,它显示使用 QUICK_TEST_MAIN_WITH_SETUP 调用设置 main.cpp 文件以初始化 QML 引擎上下文。有一条 运行time 消息:QMetaObject::invokeMethod: No such method QObject::qmlEngineAvailable(QQmlEngine*) 以及一条 qwarn 消息:QWARN : example::case_1::test_case1() file: MouseQml.qml:44: ReferenceError: mouse is not defined。 QML 测试用例找不到我的 Class 成员函数 mouse.clear()。我设置正确吗?

main.cpp

#include <QtQuickTest/quicktest.h>
#include <QQmlEngine>
#include <QQmlContext>
#include <QQmlComponent>
#include "mousememory.h"

class Setup : public QObject
{

public:
    Setup() {}

public slots:
    void qmlEngineAvailable(QQmlEngine *engine)
    {
        QScopedPointer<MouseMemory> mouse(new MouseMemory);
        engine->rootContext()->setContextProperty("mouse", mouse.data());
    }
};

QUICK_TEST_MAIN_WITH_SETUP(example, Setup)

简化MouseQML.qml

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.4

Window {
    id: root
    visible: true
    width: 500
    height: 500

    Row {
        id: tools

        Button {
            id: clear
            objectName: "clear"
            text: "Clear"
            onClicked: {
                canvas.clear()
            }
        }
    }

    Canvas {
        id: canvas
        anchors.top: tools.bottom
        width: 500
        height: 500
        property int lastX: 0
        property int lastY: 0

        function clear() {
            var ctx = getContext("2d")
            ctx.reset()
            canvas.requestPaint()
            mouse.clear()
        }

    }
}

tst_case_1.qml

import QtQuick 2.0
import QtTest 1.0
import "../app"

TestCase {
    name: "case_1"

    MouseQml {
        id: mainApp
    }

    function initTestCase() {
        var qmlClear = findChild(mainApp, "clear")
        tryCompare(qmlClear, "text", "Clear")
    }

    function cleanupTestCase() {
    }

    function test_case1() {
        var qmlClear = findChild(mainApp, "clear")
        mouseClick(qmlClear)
        //tryCompare(qmlClear, "text", "Clear")
    }

    function test_case() {

    }
}

console output

11:45:54: Starting MouseMonitor\build\ui_test\debug\ui_test...
QMetaObject::invokeMethod: No such method QObject::qmlEngineAvailable(QQmlEngine*)
********* Start testing of example *********
Config: Using QtTest library 5.11.2, Qt 5.11.2 (x86_64-little_endian-llp64 shared (dynamic) debug build; by MSVC 2015)
PASS   : example::case_1::initTestCase()
PASS   : example::case_1::test_case()
QWARN  : example::case_1::test_case1() file:///MouseQml.qml:44: ReferenceError: mouse is not defined
PASS   : example::case_1::test_case1()
PASS   : example::case_1::cleanupTestCase()
Totals: 4 passed, 0 failed, 0 skipped, 0 blacklisted, 16ms
********* Finished testing of example *********

您有 2 个错误:

  • 第一个似乎是因为文档的示例不完全正确,因为它省略了宏 Q_OBJECT 的使用,如果你想实现插槽,您应该添加 #include "main.moc".

  • 另一个因为显然你不明白QScopedPointer是做什么用的,它的主要任务是在对象也被删除的时候消除指针,这样的话QScopedPointer会在qmlEngineAvailable执行完后被删除从而消除了 MouseMemory 指针。一种可能的解决方案是使 class 的 QScopedPointer 成员延长其生命周期,从而延长 MouseMemory 的生命周期。

综合以上,解决方案如下:

main.cpp

#include "mousememory.h"

#include <QtQuickTest>
#include <QQmlEngine>
#include <QQmlContext>

class Setup : public QObject
{
    Q_OBJECT
public:
    using QObject::QObject;
public slots:

#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
    void applicationAvailable(){
    }
#endif
    void qmlEngineAvailable(QQmlEngine *engine)
    {
        mouse.reset(new MouseMemory);
        engine->rootContext()->setContextProperty("mouse", mouse.data());
    }
#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
    void cleanupTestCase(){
    }
#endif
private:
    QScopedPointer<MouseMemory> mouse;
};
QUICK_TEST_MAIN_WITH_SETUP(example, Setup)

#include "main.moc"

您找到的完整示例 here


注意:Qt 5.12 中添加了两个插槽:

  • void applicationAvailable()

  • void cleanupTestCase()