在 google mock 中执行调用函数的顺序
enforcing order of calling function in google mock
我正在学习如何使用 google mock。我观察到以下代码作为示例来指定函数调用的顺序。这里我们使用 InSequence
对象但没有在代码中的任何地方使用。请求指导我 TEST_F 在内部使用 C++ 技术的想法来使用这个对象并强制执行 oder
TEST_F(APlaceDescriptionService, MakesHttpRequestToObtainAddress) {
InSequence forceExpectationOrder;
HttpStub httpStub;
string urlStart{
"http://open.mapquestapi.com/nominatim/v1/reverse?format=json&"};
auto expectedURL = urlStart +
"lat=" + APlaceDescriptionService::ValidLatitude + "&" +
"lon=" + APlaceDescriptionService::ValidLongitude;
EXPECT_CALL(httpStub, initialize());
EXPECT_CALL(httpStub, get(expectedURL));
PlaceDescriptionService service{&httpStub};
service.summaryDescription(ValidLatitude, ValidLongitude);
}
想法很简单:如果 HttpStub::get
在 HttpStub::initialize
之前被调用,那么测试 APlaceDescriptionService.MakesHttpRequestToObtainAddress
将失败。换句话说,测试确保 HttpStub::get
在 httpStub
初始化之前不会被调用。
如果您的问题是它是如何工作的:
InSequence forceExpectationOrder
在创建时将 new Sequence
对象设置为全局对象 g_gmock_implicit_sequence
,该对象按创建顺序使用期望链。
// Points to the implicit sequence introduced by a living InSequence
// object (if any) in the current thread or NULL.
GTEST_API_ ThreadLocal<Sequence*> g_gmock_implicit_sequence;
// Creates the implicit sequence if there isn't one.
InSequence::InSequence() {
if (internal::g_gmock_implicit_sequence.get() == nullptr) {
internal::g_gmock_implicit_sequence.set(new Sequence);
sequence_created_ = true;
}
// ...
}
// Deletes the implicit sequence if it was created by the constructor
// of this object.
InSequence::~InSequence() {
if (sequence_created_) {
delete internal::g_gmock_implicit_sequence.get();
internal::g_gmock_implicit_sequence.set(nullptr);
}
}
// Adds and returns an expectation spec for this mock function.
TypedExpectation<F>& AddNewExpectation(const char* file, int line,
const std::string& source_text,
const ArgumentMatcherTuple& m) {
// ...
// Adds this expectation into the implicit sequence if there is one.
Sequence* const implicit_sequence = g_gmock_implicit_sequence.get();
if (implicit_sequence != nullptr) {
implicit_sequence->AddExpectation(Expectation(untyped_expectation));
}
return *expectation;
}
我正在学习如何使用 google mock。我观察到以下代码作为示例来指定函数调用的顺序。这里我们使用 InSequence
对象但没有在代码中的任何地方使用。请求指导我 TEST_F 在内部使用 C++ 技术的想法来使用这个对象并强制执行 oder
TEST_F(APlaceDescriptionService, MakesHttpRequestToObtainAddress) {
InSequence forceExpectationOrder;
HttpStub httpStub;
string urlStart{
"http://open.mapquestapi.com/nominatim/v1/reverse?format=json&"};
auto expectedURL = urlStart +
"lat=" + APlaceDescriptionService::ValidLatitude + "&" +
"lon=" + APlaceDescriptionService::ValidLongitude;
EXPECT_CALL(httpStub, initialize());
EXPECT_CALL(httpStub, get(expectedURL));
PlaceDescriptionService service{&httpStub};
service.summaryDescription(ValidLatitude, ValidLongitude);
}
想法很简单:如果 HttpStub::get
在 HttpStub::initialize
之前被调用,那么测试 APlaceDescriptionService.MakesHttpRequestToObtainAddress
将失败。换句话说,测试确保 HttpStub::get
在 httpStub
初始化之前不会被调用。
如果您的问题是它是如何工作的:
InSequence forceExpectationOrder
在创建时将 new Sequence
对象设置为全局对象 g_gmock_implicit_sequence
,该对象按创建顺序使用期望链。
// Points to the implicit sequence introduced by a living InSequence
// object (if any) in the current thread or NULL.
GTEST_API_ ThreadLocal<Sequence*> g_gmock_implicit_sequence;
// Creates the implicit sequence if there isn't one.
InSequence::InSequence() {
if (internal::g_gmock_implicit_sequence.get() == nullptr) {
internal::g_gmock_implicit_sequence.set(new Sequence);
sequence_created_ = true;
}
// ...
}
// Deletes the implicit sequence if it was created by the constructor
// of this object.
InSequence::~InSequence() {
if (sequence_created_) {
delete internal::g_gmock_implicit_sequence.get();
internal::g_gmock_implicit_sequence.set(nullptr);
}
}
// Adds and returns an expectation spec for this mock function.
TypedExpectation<F>& AddNewExpectation(const char* file, int line,
const std::string& source_text,
const ArgumentMatcherTuple& m) {
// ...
// Adds this expectation into the implicit sequence if there is one.
Sequence* const implicit_sequence = g_gmock_implicit_sequence.get();
if (implicit_sequence != nullptr) {
implicit_sequence->AddExpectation(Expectation(untyped_expectation));
}
return *expectation;
}