我正在學習使用 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);
}
uj5u.com熱心網友回復:
這個想法很簡單:如果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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/429502.html
上一篇:無法列印陳述句C
下一篇:openldap日志的正則運算式
