是否可以使用gmock来模拟一个包含类模板参数的函数?例如:
template <typename T>
struct Mockable
{
virtual void do_work(const int num, const T& value) = 0;
};
template <typename T>
struct MockMockable : Mockable<T>
{
MOCK_METHOD2(do_work, void(const int, const T&));
};
Run Code Online (Sandbox Code Playgroud) 如果一个接口有一个函数来创建一个删除了copy-ctor的对象,那么如何模拟这个函数呢?Gmock似乎在内部使用了对象的复制构造函数.
例如
// The object with deleted copy-ctor and copy-assignment
class TTest
{
public:
TTest() = delete;
TTest(const TTest&) = delete;
TTest& operator=(const TTest&) = delete;
TTest(TTest&&) = default;
TTest& operator=(TTest&&) = default;
explicit TTest(int) {
}
};
// My interface to mock
class MyInterface
{
public:
virtual ~MyInterface() {}
virtual TTest GetUniqueTest() = 0;
};
// The mock
class MockMyInterface: public MyInterface{
public:
MOCK_METHOD0(GetUniqueTest, TTest());
}
Run Code Online (Sandbox Code Playgroud)
编译错误说:
gmock/gmock-spec-builders.h:1330:20: error: use of deleted function 'TTest::TTest(const TTest&)'
T retval(value_);
...
gmock/gmock-actions.h:190:52: …
Run Code Online (Sandbox Code Playgroud) 我是googlemock(和StackOverflow)的新手.我MOCK_METHODn
在googlemock中使用时遇到了问题,我相信这个功能被广泛使用.这就是我做的.
我有一个Foo
虚拟重载的抽象类operator[]
:
class Foo{
public:
virtual ~Foo(){};
virtual int operator [] (int index) = 0;
}
Run Code Online (Sandbox Code Playgroud)
我想用googlemock来获得MockFoo
:
class MockFoo: public Foo{
public:
MOCK_METHOD1(operator[], int(int index)); //The compiler indicates this line is incorrect
}
Run Code Online (Sandbox Code Playgroud)
但是,这段代码给我一个像这样的编译错误:
error: pasting "]" and "_" does not give a valid preprocessing token
MOCK_METHOD1(operator[], GeneInterface&(int index));
Run Code Online (Sandbox Code Playgroud)
我的理解是编译器误解了它operator[]
并且不把它当作方法名称.但是operator[]
通过使用来模拟使用的正确方法是什么MOCK_METHODn
?我已经阅读了googlemock 的文档,但发现与我的问题无关.Cound有人帮我吗?
谢谢!
#define
如果第一次测试失败,我正在寻找一些可以停止测试用例执行的东西
TEST_F(TestInitializer, 1st-test) {
Initiator.call();
EXPECT_CALL(mock_obj, onAction(false)).Times(AtLeast(0));
// some define I want
::testing::stopIfFailed();
}
TEST_F(TestInitializer, 2nd-test) {
// this test must not be executed if first test failed
}
Run Code Online (Sandbox Code Playgroud) 我是第一次使用Google Mock(gMock).给出以下代码段:
class LinkSignals
{
public:
virtual ~LinkSignals() { }
virtual void onLink(std::string) = 0;
virtual void onUnLink() = 0;
};
class MockLinkSignals : public LinkSignals
{
public:
MOCK_METHOD1(onLink, void(std::string));
MOCK_METHOD0(onUnLink, void());
};
Run Code Online (Sandbox Code Playgroud)
MockLinkSignals mock_signals;
当我执行一些导致EXPECT_CALL(mock_signals, onLink(_))
运行的测试代码时,我如何检查参数onLink()
?
我需要编写简单的http客户端.为我的班级进行单元测试可能会很棒.但我不知道如何写出适当且可测试的课程.
例如,我有一个像这样的客户端:
class HTTPClient
{
public:
HTTPCLient(const std::string& host, const std::string& port): session(host, port) {}
void send()
{
session.sendRequest(someRequest);
Response response = session.receiveResponse();
// ...
}
private:
SomeLibrary::ClientSession session;
};
Run Code Online (Sandbox Code Playgroud)
如何测试send
方法(我真的发送我想要的东西)?我无法嘲笑它.我可以 在构造函数中编写HTTPClient
接收SomeLibrary::ClientSession
对象(在测试中我会传递模拟)但它是好的设计吗?我认为会话等的实现方式应该隐藏在我的课堂中.
你有什么主意吗?
我有一个使用类Bar的类Foo.Bar仅在Foo中使用,而Foo正在管理Bar,因此我使用unique_ptr(不是引用,因为我不需要Foo之外的Bar):
using namespace std;
struct IBar {
virtual ~IBar() = default;
virtual void DoSth() = 0;
};
struct Bar : public IBar {
void DoSth() override { cout <<"Bar is doing sth" << endl;};
};
struct Foo {
Foo(unique_ptr<IBar> bar) : bar_(std::move(bar)) {}
void DoIt() {
bar_->DoSth();
}
private:
unique_ptr<IBar> bar_;
};
Run Code Online (Sandbox Code Playgroud)
到目前为止一切顺利,这很好.但是,当我想对代码进行单元测试时,我遇到了问题:
namespace {
struct BarMock : public IBar {
MOCK_METHOD0(DoSth, void());
};
}
struct FooTest : public Test {
FooTest() : barMock{ make_unique<BarMock>() }, out(std::move(barMock)) {}
unique_ptr<BarMock> barMock; …
Run Code Online (Sandbox Code Playgroud) 我需要检测给定的函数是否已使用一组特定的参数被精确调用一次。
EXPECT_CALL(Mock_Obj, func("abc")).Times(1)
但是可以使用不同的参数多次调用该函数。
我该如何表达?
我想知道是否有一种很好的方法可以使用Google Test或Google Mock测试两个Eigen矩阵的近似相等性.
采取以下测试用例作为一个简化的例子:我乘以2点复数值矩阵A
,和B
,并期望一个确定的结果C_expect
.我C_actual = A * B
使用Eigen 计算数值结果.现在,我想比较C_expect
,和C_actual
.现在,相应的代码如下所示:
#include <complex>
#include <Eigen/Dense>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
typedef std::complex<double> Complex;
typedef Eigen::Matrix2cd Matrix;
TEST(Eigen, MatrixMultiplication) {
Matrix A, B, C_expect, C_actual;
A << Complex(1, 1), Complex(2, 3),
Complex(3, 2), Complex(4, 4);
B << Complex(4, 4), Complex(3, 2),
Complex(2, 3), Complex(1, 1);
C_expect << Complex(-5, 20), Complex(0, 10),
Complex(0, 40), Complex(5, …
Run Code Online (Sandbox Code Playgroud) 我正在尝试测试两个串口写+读取调用.第一个必须阻塞read
101ms,从而导致超时错误.第二个应该运行正常.根据谷歌模拟手册,InSequence
变量应该确保.这是代码:
{
// This variable ensures the sequence order
InSequence s;
EXPECT_CALL(serial_, Write(_, _))
.With(ElementsAreArray(command1_))
.WillOnce(Invoke(Write));
EXPECT_CALL(serial_, Read(_, _, _))
.WillOnce(Invoke(TimeoutRead));
EXPECT_CALL(serial_, Write(_, _))
.With(ElementsAreArray(command2_))
.WillOnce(Invoke(Write));
EXPECT_CALL(serial_, Read(_, _, _))
.Times(1)
.WillOnce(DoAll(SetArrayArgument<0>(response_begin, response_end),
SetArgumentPointee<2>(response_.size()),
Return(true)));
}
Run Code Online (Sandbox Code Playgroud)
TimeoutRead
只是一个导致当前线程休眠101ms的函数.但是,我不断收到此运行时错误:
[ RUN ] MasterTest.LostReply
/host/Users/blah/work/bitbucket/master_test.cc:631: Failure
Mock function called more times than expected - returning default value.
Function call: Read(0x5652c3f31120 pointing to "\xFF\xFF", 4096, 0x7ffda19d2960)
Returns: false
Expected: to be called once
Actual: called twice - …
Run Code Online (Sandbox Code Playgroud) gmock ×10
c++ ×8
googletest ×7
unit-testing ×7
googlemock ×3
testing ×2
c++11 ×1
eigen ×1
mocking ×1