我正在使用 DBUS 代理call_sync
API 从 DBUS 读取数据,现在我想添加单元测试,并为此创建了一个模拟。
这是代码:
class MockDBusProxy : public IDBusProxy {
public:
MOCK_METHOD(Glib::VariantContainerBase, callDBusMethod,
(const Glib::ustring& busName, const Glib::ustring& objectPath,
const Glib::ustring& interfaceName, const Glib::ustring& methodName,
Glib::VariantContainerBase& tuple, int timeout), (override));
};
class TestSystemOperationsTranslation : public ::testing::Test
{
public:
MockDBusProxy mockDBusProxy;
SystemOperationsTranslation testSysOperations;
TestSystemOperationsTranslation() : testSysOperations(mockDBusProxy) {}
};
bool compareVariantContainers(const Glib::VariantContainerBase& container1, const Glib::VariantContainerBase& container2) {
return container1.equal(container2);;
}
MATCHER_P(VariantContainerEq, expected, "") {
return arg.equal(expected);
}
TEST_F(TestSystemOperationsTranslation, testGetData)
{
Glib::ustring busName = "bus_name";
Glib::ustring objectPath = "object_path";
Glib::ustring interfaceName = "interface_name";
Glib::ustring methodName = "method_name";
Glib::VariantContainerBase tuple;
int timeout = 100; // Example timeout value
// Set expectations on the mock object
Glib::VariantContainerBase expectedReturnValue;
EXPECT_CALL(mockDBusProxy, callDBusMethod(busName, objectPath, interfaceName, methodName, tuple, timeout))
.WillOnce(testing::Invoke([&expectedReturnValue](const Glib::ustring&, const Glib::ustring&, const Glib::ustring&, const Glib::ustring&, const Glib::VariantContainerBase&, int) {
return expectedReturnValue;
}));
// Act
Glib::ustring result = testSysOperations.handleGetData();
std::cout << "result: " << result << std::endl;
// Validate the result using the custom comparison function
//ASSERT_TRUE(compareVariantContainers(tuple, expectedReturnValue));
EXPECT_THAT(tuple, VariantContainerEq(expectedReturnValue));
}
但我看到一个编译错误:
unit/test/gtest/googletest-src/googletest/include/gtest/gtest-matchers.h:179:60:
error: no match for ‘operator==’ (operand types are ‘const
Glib::VariantContainerBase’ and ‘const VariantContainerEq’) 179 |
bool operator()(const A& a, const B& b) const { return a == b; }
This may be due to the comparison of Glib::VariantContainerBase
objects. The Glib::VariantBase::operator== is declared as private,
causing the compilation error when the compareVariantContainers
function is used for validation.
但我不知道如何解决它。我真的可以模拟这个giomm
DBUS 代理 API 吗?