I am learning about using google mock. I have observed following code as sample to specify the order of function calls. Here we are using InSequence
object but not used any where in code. Request to guide me what idea of C technique is used internally by TEST_F to use this object and enforce the 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);
}
CodePudding user response:
The idea is simple: if HttpStub::get
is called before HttpStub::initialize
, then fail the test APlaceDescriptionService.MakesHttpRequestToObtainAddress
. In other words, the test insures that HttpStub::get
is not called before httpStub
is initialized.
If your question is how it works:
InSequence forceExpectationOrder
sets new Sequence
object to the global g_gmock_implicit_sequence
on creation, that uses a chain of expectations in the order of their creations.
// 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;
}