A simple Google Mock example

Hello!

I want to run this example: http://blog.divebomb.org/2011/07/my-first-c-cmake-googletest-and-googlemock/

But compiler write:

../HtmlParser_gtests/main.cpp:19: Failure
Actual function call count doesn't match EXPECT_CALL(mock, getUrlAsString("http://example.net"))...
Expected: to be called once
Actual: never called - unsatisfied and active


This are all files:

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <string>
#include <vector>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "HttpFetch.h"
#include "HtmlParser.h"
using ::testing::Return;

class HttpFetchMock : public HttpFetch {
public:

    MOCK_CONST_METHOD1( getUrlAsString, std::string( const std::string& ) );
};

TEST(HtmlParser, NoData) {
    char *html;
    HttpFetchMock mock;
    HtmlParser parser(mock);
    EXPECT_CALL(mock, getUrlAsString("http://example.net"))
        .WillOnce(Return(std::string(html)));
    std::vector<std::string> links = parser.getAllLinks("http://example.net");
    EXPECT_EQ(0, links.size());
}

int main( int argc, char *argv[] ) {
    ::testing::InitGoogleMock( &argc, argv );
    return RUN_ALL_TESTS( );
}


HtmlParser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef HTMLPARSER_H
#define HTMLPARSER_H

#include <string>
#include <vector>
#include "HttpFetch.h"

class HtmlParser {
public:

    HtmlParser( const HttpFetch &http ) : m_http( http ) {

    }

    std::vector<std::string> getAllLinks( const std::string &url ) const {
        // TODO
        return std::vector<std::string>();
    }

private:
    HttpFetch m_http;
};

#endif // HTMLPARSER_H 


HttpFetch.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef HTTPFETCH_H
#define HTTPFETCH_H

#include <string>

class HttpFetch {
public:

    virtual ~HttpFetch( ) {

    }

    virtual std::string getUrlAsString( const std::string &url ) const {
        // TODO
        return std::string( );
    }
};

#endif // HTTPFETCH_H 

Last edited on
You've defined HtmlParser:m_http as an HttpFetch object. So when you instantiate your HtmlParser, m_http is created as a copy of the base class part of http.

Presumably, what you want is for m_http to be a reference to the object being passed in, so that polymorphism can be used to ensure the mock methods are called.
Thank you for replay! But I don't understand how to solve it. Because I am beginner. Why does it work: http://blog.divebomb.org/2011/07/my-first-c-cmake-googletest-and-googlemock/
Last edited on
I wrote:
But compiler write:

../HtmlParser_gtests/main.cpp:36: Failure
Actual function call count doesn't match EXPECT_CALL(mock, getUrlAsString("http://example.net"))...
Expected: to be called once
Actual: never called - unsatisfied and active


Sorry! It is not a compile! It is a Google Mock Framework. Who uses Google Mock? Please, help me! I want to use GMock!
Last edited on
Please, help me!
I've already explained this. The way you're passing the mock object through to your HtmlParser, you are copying it into an object of the base class. HtmlParser:m_http is NOT going to be a HttpFetchMock, it will just be a HttpFetch.

Do you understand how polymorphism works in C++? Do you understand that it is something that applies to pointers and references?

I've already told you how to fix it:

Presumably, what you want is for m_http to be a reference to the object being passed in, so that polymorphism can be used to ensure the mock methods are called.


EDIT:OK, wait, I've just looked again, and as well as all of the above, there's a more obvious problem:

Nowhere in any of the code you've posted, is there anything that calls getUrlAsString(). You tell the mock to expect that method to be called once, but it never is.

Last edited on
Nowhere in any of the code you've posted, is there anything that calls getUrlAsString(). You tell the mock to expect that method to be called once, but it never is.


1
2
3
4
5
class HttpFetchMock : public HttpFetch {
public:

    MOCK_CONST_METHOD1( getUrlAsString, std::string( const std::string& ) );
};


Please, say me what code I need to write? I know C++ very well. How to solved my problem?
No. That's not calling getUrlAsString(). It's defining the mock method, not calling it.

Do you understand what a mock is, and what it's for?
Registered users can post here. Sign in or register to post.