google mock - 保存 EXPECT_CALL 然后重新使用和更改子句
google mock - save EXPECT_CALL then re-use and change clauses
假设你有这个:
EXPECT_CALL(MockClass_obj, f1(55)).Times(1);
// use the expectation
// ...
// Now clear it
Mock::VerifyAndClear(&MockClass_obj)
是否可以
1)保存期望
和
2) 稍后重新使用它并更改条款?
来自 here 我知道可以节省 expectations
但没有其他地方可以说明还可以做什么。
参考上面的代码,我想做这样的事情:
Expecatation exp1 = EXPECT_CALL(MockClass_obj, f1(55)).Times(1);
// use the expectation
// ...
// Now clear it
Mock::VerifyAndClear(&MockClass_obj)
// Somehow modify exp1 to change the cardinality or any of the clauses
// so that I can make the test immediately readable and convey that it's very much related to the one above":
// Instead of this:
EXPECT_CALL(MockClass_obj, f1(55)).Times(0);
// I wanna do something like
exp1.Times(0)
不,按照所描述的方式是不可能的。 Expectation class 仅用于 After
子句,仅此而已 - 通过 google-mock 设计,您不能用它做任何其他事情。
但是,通过适当的封装 - 您可能会实现目标,例如:
::testing::Expectation expectF1For55(::testing::Cardinality cardinality)
{
EXPECT_CALL(MockClass_obj, f1(55)).Times(cardinality);
}
并使用:
auto exp1 = expectF1For55(Exactly(1));
// ...
// ...
expectF1For55(Exactly(0));
不过我敢打赌,这个问题比仅仅涵盖这个例子更笼统。
例如你可以封装整个 Expectation - 无论你需要什么 - 比如:
class F1Expectation
{
public:
F1Expectation(MockClass&);
Expectation expect(int arg, ::testing::Cardinality cardinality = Exaclty(1))
{
m_arg = arg;
return EXPECT_CALL(obj, f1(m_arg)).Times(cardinality);
}
Expectation expect(::testing::Cardinality cardinality)
{
return EXPECT_CALL(obj, f1(55)).Times(cardinality);
}
private:
MockClass& obj;
Matcher<int> m_arg = _;
};
并使用:
F1Expectation exp1(MockClass_obj);
exp1.expect(1);
// ...
// ...
exp1.expect(0);
假设你有这个:
EXPECT_CALL(MockClass_obj, f1(55)).Times(1);
// use the expectation
// ...
// Now clear it
Mock::VerifyAndClear(&MockClass_obj)
是否可以
1)保存期望
和
2) 稍后重新使用它并更改条款?
来自 here 我知道可以节省 expectations
但没有其他地方可以说明还可以做什么。
参考上面的代码,我想做这样的事情:
Expecatation exp1 = EXPECT_CALL(MockClass_obj, f1(55)).Times(1);
// use the expectation
// ...
// Now clear it
Mock::VerifyAndClear(&MockClass_obj)
// Somehow modify exp1 to change the cardinality or any of the clauses
// so that I can make the test immediately readable and convey that it's very much related to the one above":
// Instead of this:
EXPECT_CALL(MockClass_obj, f1(55)).Times(0);
// I wanna do something like
exp1.Times(0)
不,按照所描述的方式是不可能的。 Expectation class 仅用于 After
子句,仅此而已 - 通过 google-mock 设计,您不能用它做任何其他事情。
但是,通过适当的封装 - 您可能会实现目标,例如:
::testing::Expectation expectF1For55(::testing::Cardinality cardinality)
{
EXPECT_CALL(MockClass_obj, f1(55)).Times(cardinality);
}
并使用:
auto exp1 = expectF1For55(Exactly(1));
// ...
// ...
expectF1For55(Exactly(0));
不过我敢打赌,这个问题比仅仅涵盖这个例子更笼统。
例如你可以封装整个 Expectation - 无论你需要什么 - 比如:
class F1Expectation
{
public:
F1Expectation(MockClass&);
Expectation expect(int arg, ::testing::Cardinality cardinality = Exaclty(1))
{
m_arg = arg;
return EXPECT_CALL(obj, f1(m_arg)).Times(cardinality);
}
Expectation expect(::testing::Cardinality cardinality)
{
return EXPECT_CALL(obj, f1(55)).Times(cardinality);
}
private:
MockClass& obj;
Matcher<int> m_arg = _;
};
并使用:
F1Expectation exp1(MockClass_obj);
exp1.expect(1);
// ...
// ...
exp1.expect(0);