A simple Google Mock example

4,675 views
Skip to first unread message

Ivan Enzhaev

unread,
May 28, 2014, 1:44:28 PM5/28/14
to googl...@googlegroups.com
Hello!


But I see the text:

../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
[code]
#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( );
}
[/code]

HtmlParser.h
[code]
#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
[/code]

HttpFetch.h
[code]
#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
[/code]

Keith Ray

unread,
May 28, 2014, 2:42:23 PM5/28/14
to Ivan Enzhaev, googl...@googlegroups.com
In the code you published in this email, there is no code calling "getUrlAsString."

In the code published in "my-first-c-cmake-googletest-and-googlemock" there is no code calling "GetUriAsString".

That webpage maybe using a "mock-first" TDD example: first you write a failing test, then you make the test pass. In this case, the test is failing because the mocked method is never called. 

I think this would be a bad example of TDD. In TDD, I don't write tests that fail because a method isn't called. I write tests that fail because code doesn't yet exist to create the result or state expected by the test.

--

---
You received this message because you are subscribed to the Google Groups "Google C++ Mocking Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googlemock+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/googlemock/f889edc4-ebe0-4834-af01-b1643346f478%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ivan Enzhaev

unread,
May 29, 2014, 12:56:31 AM5/29/14
to Keith Ray, googl...@googlegroups.com
Sorry for my English in advance.

What does this mean? "In the code you published in this email, there is no code calling "getUrlAsString.""

I have this method in the class HttpFetch:


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

It returns empty string.

Please, see this code:

TEST(HtmlParser, NoData) {
    char *html;
    HttpFetchMock mock;
    HtmlParser parser(mock);
    EXPECT_CALL(mock, getUrlAsString("http://example.net"))
        .WillOnce(Return(std::string(html)));
}

class Mock has a method called "getUrlAsString". This string ".WillOnce" means that we will call this method one time. This string "Return(std::string(html)" means that this method return empty string.

Billy Donahue

unread,
May 29, 2014, 1:02:00 AM5/29/14
to Ivan Enzhaev, Keith Ray, googl...@googlegroups.com
On Thu, May 29, 2014 at 12:23 AM, Ivan Enzhaev <8obse...@gmail.com> wrote:
Sorry for my English in advance.

What does this mean? "In the code you published in this email, there is no code calling "getUrlAsString.""

I have this method in the class HttpFetch:


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

It returns empty string.

Please, see this code:

TEST(HtmlParser, NoData) {
    char *html;
    HttpFetchMock mock;
    HtmlParser parser(mock);
    EXPECT_CALL(mock, getUrlAsString("http://example.net"))
        .WillOnce(Return(std::string(html)));
}

class Mock has a method called "getUrlAsString". This string ".WillOnce" means that we will call this method one time.

That's setting up an expectation that getUrlAsString will be called, but Keith is correct that there is no code that actually calls getUrlAsString here.
 
This string "Return(std::string(html)" means that this method return empty string.

Not quite. The char* html is never initialized, so there's no telling what string will be returned, if any.


 

Ivan Enzhaev

unread,
May 29, 2014, 9:21:27 AM5/29/14
to Billy Donahue, Keith Ray, googl...@googlegroups.com
It is crashed on the string: EXPECT_STREQ("http://example.net/index.html", links[0].c_str());


On Thu, May 29, 2014 at 12:48 PM, Ivan Enzhaev <8obse...@gmail.com> wrote:
Now I see this text and my application is crashed:

[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from HtmlParser
[ RUN      ] HtmlParser.OneLink
../HtmlParser_gtests/main.cpp:28: Failure
Value of: links.size()
  Actual: 0
Expected: 1
The program has unexpectedly finished.

#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, OneLink) {
    char *html = "<html>"
            "<head></head>"
            "<body>"
            "<a href='/index.html'>index.html</a>"
            "</body>"
            "</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(1, links.size());
    EXPECT_STREQ("http://example.net/index.html", links[0].c_str());
}

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

Ivan Enzhaev

unread,
May 29, 2014, 9:21:27 AM5/29/14
to Billy Donahue, Keith Ray, googl...@googlegroups.com
Now I see this text and my application is crashed:

[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from HtmlParser
[ RUN      ] HtmlParser.OneLink
../HtmlParser_gtests/main.cpp:28: Failure
Value of: links.size()
  Actual: 0
Expected: 1
The program has unexpectedly finished.

#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, OneLink) {
    char *html = "<html>"
            "<head></head>"
            "<body>"
            "<a href='/index.html'>index.html</a>"
            "</body>"
            "</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(1, links.size());
    EXPECT_STREQ("http://example.net/index.html", links[0].c_str());
}

//TEST(HtmlParser, NoData) {
//    char *html;
//    HttpFetchMock mock;
//    HtmlParser parser(mock);
//    EXPECT_CALL(mock, getUrlAsString("http://example.net"))
//        .WillOnce(Return(std::string("")));
//    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( );
}

On Thu, May 29, 2014 at 9:01 AM, Billy Donahue <billyd...@google.com> wrote:

Billy Donahue

unread,
May 29, 2014, 9:27:43 AM5/29/14
to Ivan Enzhaev, Keith Ray, googl...@googlegroups.com
On Thu, May 29, 2014 at 4:53 AM, Ivan Enzhaev <8obse...@gmail.com> wrote:
It is crashed on the string: EXPECT_STREQ("http://example.net/index.html", links[0].c_str());


On Thu, May 29, 2014 at 12:48 PM, Ivan Enzhaev <8obse...@gmail.com> wrote:
Now I see this text and my application is crashed:

[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from HtmlParser
[ RUN      ] HtmlParser.OneLink
../HtmlParser_gtests/main.cpp:28: Failure
Value of: links.size()
  Actual: 0
Expected: 1
The program has unexpectedly finished.

You're accessing links[0] but links is empty.
You still haven't provided any code that calls mock.getUrlAsString.

Ivan Enzhaev

unread,
May 29, 2014, 12:11:51 PM5/29/14
to Billy Donahue, Keith Ray, googl...@googlegroups.com
Please, give me a simple example about TDD in Google Mock. I can use GTest very well. But I want to use GMock too.

Keith Ray

unread,
May 29, 2014, 1:32:32 PM5/29/14
to Ivan Enzhaev, Billy Donahue, googl...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages