Gmock: Sequence calls inside of a block with arbitrary block order

Consider this interface:

class Interface
{
public:
    virtual ~Interface = default;

    virtual void open(int id) = 0;
    virtual void close(int id) = 0;

protected:
    // Default other special members here
};

Note that close() may only be called after an open() and the same ID must be used for both. Also, close() must have been called before the next call to open(). IDs should not repeat.

The following function uses the interface.

void actWithId(Interface & interface, int const id)
{
    interface.open(id);
    interface.close(id);
}

Now I got some hard coded IDs and I need to call actWithId() for every such index.

void act(Interface & interface)
{
    actWithId(interface, 1);
    actWithId(interface, 2);
    actWithId(interface, 3);
}

The order of the calls however is not important, so the following code would be acceptable as well:

void act(Interface & interface)
{
    actWithId(interface, 2);
    actWithId(interface, 3);
    actWithId(interface, 1);
}

I now want to write tests with GTest/GMock in such a way that the tests are robust against a order changing refactoring. To be explicit, the tests should:

  1. still succeed if I changed the order of the calls to actWithId()
  2. break if the open/close calls are out of order. For example the following call sequence on an Interface instance should break them: interface.open(1); interface.open(2); interface.close(1); interface.close(2);

I’m skipping the mock definition (it should be obvious) and assume a using namespace ::testing; for the following code snippets.

To achieve requirement 2 I could use an InSequence object like so:

MockInterface mock;
InSequence const seq;
EXPECT_CALL(mock, open(1));
EXPECT_CALL(mock, close(1));
EXPECT_CALL(mock, open(2));
EXPECT_CALL(mock, close(2));
EXPECT_CALL(mock, open(3));
EXPECT_CALL(mock, close(3));
act(mock);

But this does not fulfill requirement 1, the second implementation of act() will break this test.
Instead I could use a Sequence objects like so:

MockInterface mock;
Sequence const seq1, const seq2, const seq3;
EXPECT_CALL(mock, open(1)).InSequence(seq1);
EXPECT_CALL(mock, close(1)).InSequence(seq1);
EXPECT_CALL(mock, open(2)).InSequence(seq2);
EXPECT_CALL(mock, close(2)).InSequence(seq2);
EXPECT_CALL(mock, open(3)).InSequence(seq3);
EXPECT_CALL(mock, close(3)).InSequence(seq3);
act(mock);

This will accept both implementations of act() thus fulfilling requirement 1 but it will also accept the counter-example in requirement 2. This is because I did not tell GMock that each sequence must be finished before the next is started.

The best I can come up with is introducing state to track the current id and throw if it does not match:

MockInterface mock;
std::optional<int> currentId;
EXPECT_CALL(mock, open(_)).WillRepeatedly(Invoke([&](int const id)
    {
        if (currentId.has_value())
        {
            throw std::runtime_error{ "Calling open() but is not closed" };
        }
    }));
EXPECT_CALL(mock, close(_)).WillRepeatedly(Invoke([&](int const id)
    {
        if (!currentId.has_value())
        {
            throw std::runtime_error{ "Calling close() but is not opened" };
        }
        if (*currentId != id)
        {
            throw std::runtime_error{ "Calling close() but was opened with a different id" };
        }
    })); 

But in reality there are some more calls between the open()/close()-pairs and I would have to set up a similar action for all of them, which also kind of feels like partly re-implementing the logic. If I wanted to avoid duplicate IDs I would need to keep track of those as well which further complicated things.

Any ideas how to achieve this within the GMock framework? Ideally with one of the sequencing operations like .InSequence() or .After().

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật