1

在 TestGroup_Person 中,当我从 JB_PersonDao 检索 QSharedPointer<-JB_TableRowProt> 并将其分配给 QSharedPointer<-JB_TableRowProt> aGroup_Person(在 .h 中)时,然后我在 TestGroup_Person 的方法中收到此错误。

或者,如果我在每个方法中从 JB_DaoProt 检索 QSharedPointer<-JB_TableRowProt>(并且不将其分配给 QSharedPointer<-JB_TableRowProt> aGroup_Person),它工作正常。

有人可以向我解释为什么这个作业似乎导致了错误吗?

我收到了这个错误:

QFATAL : TestGroup_Person::test_getDBTable_Name() Received signal 11
         Function time: 0ms Total time: 0ms
FAIL!  : TestGroup_Person::test_getDBTable_Name() Received a fatal error.

这是代码:主要:

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QStringList arguments = QCoreApplication::arguments();

    QFile aFile(DATABASE_FILENAME);  // remove existing SP3.db
    if (aFile.exists())
        aFile.remove();

    map<QString, unique_ptr<QObject>> tests;
    tests.emplace("group_person", make_unique<TestGroup_Person>());


    if (arguments.size() >= 3 && arguments[1] == "-select") {
        QString testName = arguments[2];
        auto iter = tests.begin();
        while(iter != tests.end()) {
            if (iter->first != testName) {
                iter = tests.erase(iter);
            } else {
                ++iter;
            }
        }
        arguments.removeOne("-select");
        arguments.removeOne(testName);
    }

    int status = 0;

        for(auto& test : tests) {
            status |= QTest::qExec(test.second.get(), arguments);
        }

    return status;
    }

TestGroup_Person.h

#include <QString>
#include <QtTest>
#include <QSharedPointer>

#include "A_DB_Classes/JB_DatabaseManager.h"
#include "A_Tables/JB_Group_Person.h"

class TestGroup_Person : public QObject
{
    Q_OBJECT

public:
    TestGroup_Person();

private Q_SLOTS:
    void test_allFieldsEqualsFieldNames();
    void test_getDBTable_FieldNames();
    void test_getDBTable_Name();
    void test_hasJoinTable();
    void test_setValueForField();
    void test_setStorageValueForField();
    void test_getStorageFields();

private:
    JB_DatabaseManager& mDB;
    QSharedPointer<JB_TableRowProt> aGroup_Person;

};

TestGroup_Person.cpp:

TestGroup_Person::TestGroup_Person():
mDB(JB_DatabaseManager::instance())
{
    aGroup_Person = mDB.aPersonDao.getJoinTableObject();
}

void TestGroup_Person::test_allFieldsEqualsFieldNames()
{
    const QVector<QString>& aVector = aGroup_Person->getDBTable_FieldNames();
    const QList<QString> aList = aGroup_Person->getAllFieldNames();
    bool isThere = true;
    QString fieldName = "";
    QString aResult = "This field name does not exist in all_FieldNamesAndValuesH";
    for (int i = 0; i < aVector.size() && isThere; ++i) {
        fieldName = aVector.at(i);
        isThere = aList.contains(aVector.at(i));
    }
    if (isThere)
        aResult = fieldName;
    QCOMPARE(fieldName, QString(aResult));
}

void TestGroup_Person::test_getDBTable_FieldNames()
{
    const QVector<QString>& aVector = aGroup_Person->getDBTable_FieldNames();
    QString fieldName = "";
    for (int i = 0; i < aVector.size(); ++i) {
        fieldName = fieldName + aVector.at(i) +",";
    }

    QVector<QString> fieldNames;
    QVector<QString> columnHeadings;
    aGroup_Person->getViewableFieldNamesAndHeadings(fieldNames, columnHeadings);
    bool correct = fieldNames.count() == columnHeadings.count();
    if (!correct)
        correct = columnHeadings.count() == 0;

    QCOMPARE(correct, true);
    QCOMPARE(fieldName,     QString("groupID,personID,jobID,grp_per_dateTimeStamp,"));
}

void TestGroup_Person::test_getDBTable_Name()
{
    QCOMPARE(aGroup_Person->getDBTable_Name(), QString("Group_Person"));
}

JB_DaoProt的相关方法:

QSharedPointer<JB_TableRowProt> JB_DaoProt::getJoinTableObject()
{
    JB_TableRowProt* aJoinTable = new_JoinTableRowProt();
    QSharedPointer<JB_TableRowProt> pJoinTable(aJoinTable);
    return pJoinTable;
}
4

0 回答 0