doctest icon indicating copy to clipboard operation
doctest copied to clipboard

Add a BDD style 'EXPECT' clause as in GIVEN/EXPECT/WHEN to be aligned with mock frameworks constraints

Open boutblock opened this issue 4 years ago • 4 comments

Most, if not all, mock frameworks out there require to declare the expected callbacks BEFORE executing the stimulus on the SUT. This defers from the regular pattern of GIVEN/WHEN/THEN where the expectations are checked AFTER the stimulus.

So to be more readable, one would prefer to complete the pattern with something like GIVEN[/EXPECT]/WHEN[/THEN]. This can be achieved by adding a new define like this:

#define EXPECT(name)     DOCTEST_SUBCASE("  Expect: " name)
#define AND_EXPECT(name) DOCTEST_SUBCASE("     And: " name)

Example of usage with gmock:

SCENARIO("example of mock usage") {
  GIVEN("an initial state") {
     NaggyMock<MyMock> myMock;
     MyClass sut(myMock);
     EXPECT("my mock callback to be called once") {
       EXPECT_CALL(myMock, myCallback());
       WHEN("the SUT is stimulated") {
         sut.callMeMaybe();
      }
    }
  }
}

boutblock avatar Nov 18 '20 11:11 boutblock

This and the other BDD-related issue will go into the next release. Thanks for reporting!

onqtam avatar Nov 18 '20 13:11 onqtam