d3r0n
d3r0n

Reputation: 128

Mocking class to test its methods

I want to test makeTvSeries() method without extracting getNumberOfShows, printMsg to other class and then mocking it so I thougth about mocking TvChannel class.

Is it possible to call base class method (makeTvSeries) which will call child methods: getNumberOfShows, printMsg without getting rid of virtuality? So I could use same mock class definiton in other tests for instance foo.playTvSeries(mockTvChannel) and expect calls to makeTvSeries?.

Moreover is it good practice what I am doing at all? In whole program there are also other classes which use cin and cout and as I said at the begining I didn't wanted to extract all of them to one class responsible for input/output. What are Yours experiences and what I should do ?

class MockTvChannel : public TvChannel{
public:
    MOCK_METHOD0(getNumberOfShows, int());
    //MOCK_METHOD0(makeTvSeries, void());
    MOCK_METHOD0(printMsg, void());
};

TEST(sample_test_case, sample_test)
{
    MockTvChannel channel;
    EXPECT_CALL(channel, getNumberOfShows())
        .Times(1)
        .WillOnce(::testing::Return(10));

    EXPECT_CALL(channel, printMsg())
        .Times(10);

    channel.makeTvSeries();
}    


class TvChannel
{
protected:
    virtual int getNumberOfShows(){
        int nShows;
        std::cin >> nShows;
        return nShows;
    }

    virtual void printMsg(){
        std::cout << "What a show!" << std::endl;
    }

public:
    /*virtual*/ void makeTvSeries()
    {
        int nShows = getNumberOfShows();
        for(int i = 0; i<nShows; ++i){  
            printMsg();
        }
    }
    virtual ~TvChannel() {};
};

Upvotes: 1

Views: 108

Answers (1)

BЈовић
BЈовић

Reputation: 64293

So I could use same mock class definiton in other tests for instance foo.playTvSeries(mockTvChannel) and expect calls to makeTvSeries?

Yes, you can. And your implementation is fine.

is it good practice what I am doing at all?

It is. Following the SOLID principle, you applied next principles :

  • LSP : in unit test you switched real implementation and tested using mock, so your makeTvSerier works
  • ISP : your class has an interface
  • DIP : I guess, you want to use inversion of control when you pass instance of mock to other objects

Upvotes: 1

Related Questions