我有一个成功运行的简单应用程序;但是,我在尝试使用qmltest运行测试用例时遇到了困难。在documentation中,它显示了使用QUICK_TEST_MAIN_WITH_SETUP
调用来设置main.cpp文件,以初始化QML引擎上下文。这里有一个运行时消息: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测试用例找不到我的类成员函数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 *********
发布于 2018-12-11 04:23:44
您有两个错误:
第一个问题似乎是因为文档的例子并不完全正确,因为它省略了宏Q_OBJECT
的使用,如果你想实现插槽,你应该添加
考虑到上述情况,解决方案如下:
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()
https://stackoverflow.com/questions/53711633
复制相似问题