googlemock 相关问题

考虑到C ++的具体细节,Google C ++ Mocking Framework(或简称Google Mock)是一个用于编写和使用C ++模拟类的库。

如何将额外的参数传递给google模拟EXPECT_CALL

我正在测试通过接口使用依赖项的 C++ 类。我已经模拟了该接口,并有一个复杂的 lambda,我在 GMock 的 EXPECT_CALL 中使用它来验证我的类是否调用了模拟的 fu...

回答 1 投票 0

EXPECT_CALL 返回“实际:从未调用过 - 不满意且处于活动状态”

我有以下场景,我调用 EXPECT_CALL 将函数 Foo 的返回值设置为 true 但出现以下错误 实际:从未致电 - 不满意并采取行动...

回答 2 投票 0

如何构建 google 测试和 google 模拟库并在 VS 控制台应用程序中使用它们?

如何创建基于 google test 的单元测试项目,将最新的 gtest.lib 和 gmock.lib 集成到控制台应用程序或空项目中(不想使用 Visual Studio google 测试项目

回答 1 投票 0

如何使用 gmock 匹配 C++ 元组中的一个元素?

如何使用 gmock 匹配 C++ 元组中的一个元素? 例如,让我们尝试从 std::tuple 中提取 std::string 。 我知道我可以编写一个自定义匹配器,例如...

回答 2 投票 0

通过MOCK_METHOD模拟类时,是否需要override关键字?

可以说我有 类 Foo { 民众: VIRTUAL_FOR_TEST 无效栏(int i); }; 它被嘲笑的地方 类 FooMock : 公共 Foo { 民众: MOCK_METHOD(void, bar, (int), ()); }; Foo 应该只是...

回答 1 投票 0

Google 测试 Expect 来自函数的调用

假设我有一个简单的模拟测试。 #include“boost/interprocess/detail/interprocess_tester.hpp” #包括 #包括 使用命名空间::testing;

回答 1 投票 0

使用 gtest 测试来自 MOCK_METHOD 调用的 std::vector 参数

有没有办法将向量引用中的数据存储到 MOCK_METHOD 中? 我有以下模拟界面: MOCK_METHOD(bool, SetData, (const std::vector& data), (override)); ...

回答 2 投票 0

如何使用gmock MOCK_METHOD进行重载运算符?

我是 googlemock (和 StackOverflow)的新手。我在 googlemock 中使用 MOCK_METHODn 时遇到问题,我相信这个函数被广泛使用。这就是我所做的。 我有一个抽象类 Foo ,带有 v...

回答 1 投票 0

EXPECT_CALL() 宏在尝试抛出自定义异常时无法编译

我有以下代码 test.cpp ,无法编译(使用 g++ test.cpp -lgtest -lgmock -pthread -lfmt 编译),我收到此错误: 无法转换‘std::forward 我有以下代码 test.cpp ,无法编译(使用 g++ test.cpp -lgtest -lgmock -pthread -lfmt 编译),我收到此错误: 无法将‘std::forward从‘CustomException’转换为‘fmt::v8::format_string<>’ 这段代码的正确版本应该是什么:)? #include <gtest/gtest.h> #include <gmock/gmock.h> #include <stdexcept> #include <fmt/core.h> using ::testing::_; using ::testing::Throw; class CustomException : public std::exception { public: template <typename... Args> explicit CustomException(Args&&... args) : _exception_string{fmt::format(std::forward<Args>(args)...) + _exception_suffix} { } inline const char* what() const noexcept override { return _exception_string.c_str(); } private: std::string _exception_suffix{" Custom exception..."}; std::string _exception_string{}; }; class Calculator { public: virtual double Divide(double a, double b) { if (b == 0.0) { throw std::invalid_argument("Division by zero"); } return a / b; } }; // Mock class for Calculator class MockCalculator : public Calculator { public: MOCK_METHOD(double, Divide, (double a, double b), (override)); }; TEST(CalculatorTest, DivideWithExceptions) { MockCalculator mockCalculator; EXPECT_CALL(mockCalculator, Divide(10.0, 0.0)) .WillOnce(Throw(CustomException("Division by zero"))); // Test division by zero try { double result = mockCalculator.Divide(10.0, 0.0); FAIL() << "Expected exception not thrown."; } catch (const std::invalid_argument& e) { EXPECT_STREQ(e.what(), "Division by zero"); } } int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } 这样的构造函数将起作用: template <typename... Args> explicit CustomException(std::string_view fmt, Args&&... args) : _exception_string{fmt::format(fmt, std::forward<Args>(args)...) + _exception_suffix} { } 我填写它与P2216R3有关,通过参数展开收到的格式字符串不是constexpr。不幸的是我没有时间在文章中进行北斗潜水。

回答 1 投票 0

如何让 gmock 对象返回固定的 std::forward_list

我正在尝试编写这个测试: 测试(AccountServiceShould,print_a_statement_containing_all_transactions){ 自动 transactionRepository = 新 TransactionRepositoryMock; std::forward_list 我正在尝试编写这个测试: TEST(AccountServiceShould, print_a_statement_containing_all_transactions) { auto transactionRepository = new TransactionRepositoryMock; std::forward_list<model::Transaction *> transactionList; auto statementPrinter = new StatementPrinterMock; transactionList.assign({transaction("22/12/2019", 1000)}); ON_CALL( *transactionRepository, all() ) .WillByDefault(Return(transactionList)); Clock *myClock = new Clock; auto accountService = new AccountService(transactionRepository, myClock); EXPECT_CALL(*statementPrinter, print(Eq(transactionList))); accountService->printStatement(); delete accountService; delete transactionRepository; delete myClock; delete statementPrinter; } 我在编译时遇到错误: No viable conversion from 'internal::ReturnAction<forward_list<Transaction *, allocator<Transaction *>>>' to 'const Action<std::forward_list<model::Transaction, std::allocator<model::Transaction>> ()>' candidate template ignored: requirement 'internal::disjunction<std::is_constructible<std::function<std::forward_list<model::Transaction, std::allocator<model::Transaction>> ()>, testing::internal::ReturnAction<std::forward_list<model::T... candidate template ignored: could not match 'Action' against 'ReturnAction' explicit constructor is not a candidate candidate template ignored: could not match 'OnceAction' against 'Action' candidate template ignored: requirement 'conjunction<testing::internal::negation<std::is_same<void, std::forward_list<model::Transaction, std::allocator<model::Transaction>>>>, testing::internal::negation<std::is_reference<std::forward_... passing argument to parameter 'action' here 我不明白问题出在哪里。我尝试更改 transactionList 的类型,使用动态对象,甚至创建了自己的 MATCHER_P,但我无法弄清楚这个。 我不是 C++ 专家,恰恰相反。 谢谢! 应该是评论,但是字符太多了。 没有可行的转换 forward_list<Transaction *, allocator<Transaction *>> 到 std::forward_list<model::Transaction, std::allocator<model::Transaction>> 所以,我90%(因为ofc你没有在问题中包含相关信息)确定你有一个拼写错误,因为你的模拟返回std::forward_list<Transaction>并且你尝试返回std::forward_list<Transation*>。 编译没有问题: struct Transaction {}; class TransactionRepositoryMock { public: MOCK_METHOD(std::forward_list<Transaction*>, all, ()); }; TEST(AccountServiceShould, print_a_statement_containing_all_transactions) { auto transactionRepository = new TransactionRepositoryMock; std::forward_list<Transaction*> transactionList; transactionList.assign({new Transaction}); ON_CALL(*transactionRepository, all()).WillByDefault(Return(transactionList)); }

回答 1 投票 0

自动生成Google Mock方法?

我对 C++ 和单元测试相当陌生,我现在正在学习使用 Google Mock 和 Google Test 来测试我正在使用的一些代码。而不是编写所有 Google Mock 方法手册...

回答 3 投票 0

如何使用 gmock 创建模板化自定义匹配器

我想使用 WhenDynamicCastTo(m) 来自:https://google.github.io/googletest/reference/matchers.html#pointer-matchers 但我想要动态转换的是在shared_ptr中并且

回答 1 投票 0

Lambda 读取 constexpr 值而不在 gmock 匹配器中捕获它

根据Lambda表达式: 如果变量 (...) 是 constexpr 并且没有可变成员,则 lambda 表达式可以读取变量的值而不捕获它 在下面的例子中,我认为...

回答 1 投票 0

google 测试:MOCK_METHODn(旧式)- 带有 char** 参数的函数

我正在使用旧版本的谷歌测试,使用 MOCK_METHODn(旧式)宏。我正在尝试为带有 char** 参数的虚函数创建模拟方法并收到编译错误。 做...

回答 1 投票 0

使用 Gmock 模拟重载方法

问候所以我有一个如下所示的界面 A类接口{ 民众: /// =A 可以处于的不同状态 枚举类状态{ 闲置的, 忙碌的 }; 结构 AMetad...

回答 1 投票 0

Google 测试:检查循环中发生的函数调用顺序

我正在尝试使用 Google Test 检查函数调用(模拟方法)的顺序,但是当函数调用循环发生时我遇到了问题。在下面的例子中,DummyClass 是 moc...

回答 1 投票 0

如何使用谷歌测试的容器匹配器来检查容器中是否存在值对象?

我在 C# 中进行了此练习,我正试图将其转换为 C++。 我想像在 C# 中一样使用匹配器进行一些谷歌测试来检查集合内容。容器中的元素...

回答 2 投票 0

如何读取传递给使用 googlemock 模拟的方法的数组指针的内容?

我正在将一个指向数组的指针传递给一个函数,我正在用 googlemock 模拟它。我想验证参数的内容,这适用于标量,但我只能获取...

回答 1 投票 0

如何使用 Google Mock 模拟模板化方法?

我正在尝试模拟模板化方法。 这是包含 mock 方法的类: 我的班级 { 民众: 虚拟 ~myClass() {} 模板 void myMethod(T par...

回答 3 投票 0

googletest gmock:测试 C 风格的回调函数

需要使用 gooogletest gmock 测试 C 风格回调函数的建议。 测试用 std::function 定义的回调很容易。 这两个主题很有帮助: 使用谷歌测试来检查电话...

回答 0 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.