Kata Step Four

We don’t like the duplication in our tests. Google Test framework allows us to create a test fixture, so we can share a little setup between tests.

Uncomment the test fixture in BoundedQueueTest.cpp and implement as follows:

BoundedQueueTest.cpp

class BoundedQueueTest : public ::testing::Test {
public:
        MockQueueControl producer;
        MockQueueControl consumer;
        BoundedQueueTest() : q(producer, consumer) {}
        BoundedQueue<int> q;
};

Now we refactor our tests to rely on the fixture.

Important Note -> Tests that use a fixture use a different macro than standalone tests. We need to change each of our TEST() macros to TEST_F().

BoundedQueueTest.cpp

TEST_F(BoundedQueueTest, CreateResumesProducer) {
        EXPECT_CALL(producer, Resume());
        BoundedQueue<int> q(producer, consumer);
}

TEST_F(BoundedQueueTest, CreatePausesConsumer) {
        EXPECT_CALL(consumer, Pause());
        BoundedQueue<int> q(producer, consumer);
}

TEST_F(BoundedQueueTest, EnqueueResumesConsumer) {
        EXPECT_CALL(consumer, Resume());
        q.enqueue(5);
}

The tests should still pass.

Question: Why do we still need to instantiate the BoundedQueue in the first two tests? After all, we're instantiating it in the setup fixture.

See if you can answer this question, or read on:











Answer: Because we are expecting certain calls to happen during the instantiation. We need to set the expectation before we call the code that should fulfill the expectation.




Go on to BoundedQueueKataStepFive.... or go back

BoundedQueueKataStepFour (last edited 2010-01-17 00:47:29 by KayJohansen)