rollbear / trompeloeil

Header only C++14 mocking framework
Boost Software License 1.0
811 stars 86 forks source link

Question about expectations #282

Closed Swassie closed 2 years ago

Swassie commented 2 years ago

Hey! I have a question about the following code snippet:

class Test_fixture
{
public:
    void open_file(const fs::path& path) {
        <more expectations>
        ALLOW_CALL(m_mock, show_view());

        m_mock.open_files_clicked();
    }

    Mock m_mock;
};

TEST_CASE_METHOD(Test_fixture, "Opening the first file shows the view") {
    REQUIRE_CALL(m_mock, show_view());
    open_file(data_path / "file");
}

In my test helper open_file I setup some expectations and use ALLOW_CALL for show_view since it's not guaranteed to be called. Then I have a test case where I want to make sure that show_view is called, but REQUIRE_CALL fails since the other one is added later. Is this possible somehow, to have a wide expectation in a helper function and override it with a narrow expectation in a test case? I could create another helper function that just creates the wide expectations, so I can add the narrow after, but I wanted to check if there was any other options.

Thanks.

rollbear commented 2 years ago

The REQUIRE_CALL would have to be in a narrower scope than the ALLOW_CALL, which would change your design quite a bit.

I think I'd prefer to let the fixture have a counter for the number of times show_view() has been called. In the situations where it's important that it's called, you can use a normal test framework assertion to make sure that the counter has incremented.

Another technique, if it's important for you that an error is caught by a mock, is to add another constraint on the ALLOW_CALL, that you can programmatically make a not match.

class Test_fixture
{
public:
    void open_file(const fs::path& path) {
        <more expectations>
        ALLOW_CALL(m_mock, show_view())
          .WITH(relaxed);

        m_mock.open_files_clicked();
    }
    bool relaxed = true;
    Mock m_mock;
};

TEST_CASE_METHOD(Test_fixture, "Opening the first file shows the view") {
    relaxed = false;
    REQUIRE_CALL(m_mock, show_view());
    open_file(data_path / "file"); // `ALLOW_CALL` will not match, but `REQUIRE_CALL` above will.
}
Swassie commented 2 years ago

Thanks for the help and your work on the library.