Why do you need to call both replayAll and verifyAll when using Google Closure mocks?

665 views Asked by At

Reading code using Closure's Mocks and am a big confused by the syntax. Many look like this:

  mockChart();

  // Test
  this.mockControl.$replayAll();
  this.mainMethod(testData);

  // Verify
  this.mockControl.$verifyAll();
  // lots of asserts

I don't understand why one would call both replay and then later verify. It sounds like replay is actually doing the work of record, which I would have expected to have started already.

The flow is a bit different from Mockito, the only other framework I'm familiar with, and I haven't found good documentation at this level (just class-level jsdoc).

1

There are 1 answers

0
Harry Cutts On

You can think of there being two stages when mocking something with Closure.

During the first, the test informs the mock framework of what calls are expected and how it should respond to them. Calling mockFoo.doTheThing() during this stage will add an expected call to doTheThing on the Foo mock.

During the second, the mock framework records calls while the test is running. Calling mockFoo.doTheThing() during this stage will record the fact that doTheThing was called, and possibly run some test code that was added in the first stage.

The first stage starts when the MockControl object is created, and ends when $replayAll is called. The second stage starts when $replayAll is called and ends when $verifyAll is called, at which point the mock framework checks that it all of the expected method calls were made.