gmock/google-mock发出警告并通过模拟异常使测试失败 [英] gmock/google-mock issues warning and fails the test with mocking exceptions

查看:545
本文介绍了gmock/google-mock发出警告并通过模拟异常使测试失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Google模拟程序编写了一个演示模拟程序.问题是它失败了并且没有正确地模拟.我在这里不明白这个问题.

I have coded a demo mock using google mock. The issue is that it is failing and not properly mocking. I cannot understand the issue here.

代码:

test/mock_turtle_test.cc

test/mock_turtle_test.cc

#include "mock_turtle.h"
#include "../painter.h"
#include <gtest/gtest.h>

using  ::testing::_;
using  ::testing::AtLeast;
using  ::testing::Return;
ACTION(MyException)
{
        throw(error_k());
}
TEST(PainterTest, CanDrawSomething) {
  Painter painter;
  EXPECT_CALL(*(painter.getTurtle()), PenDown())
      .Times(AtLeast(1))
      .WillOnce(MyException())
      .WillRepeatedly(Return(5));
  EXPECT_TRUE(painter.DrawCircle(0, 0, 10));
}

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

inc/mock_turtle.h

inc/mock_turtle.h

#pragma once
#include <cstdlib>
#include "turtle.h"
#include <gmock/gmock.h>

class Turtle : public FakeTurtle{
 public:
  using FakeTurtle::FakeTurtle;
  MOCK_METHOD((void), PenUp, ());
  MOCK_METHOD((int), PenDown, ());
  MOCK_METHOD((void), Forward, (int distance));
  MOCK_METHOD((void), Turn, (int degrees));
  MOCK_METHOD((void), GoTo, (int x, int y));
  MOCK_METHOD((int), GetX, ());
  MOCK_METHOD((int), GetY, ());
};

inc/turtle.h

inc/turtle.h

#pragma once

class FakeTurtle {

public:
  virtual ~FakeTurtle() {}
  virtual void PenUp() {};
  virtual int PenDown() {};
  virtual void Forward(int distance) {};
  virtual void Turn(int degrees) {};
  virtual void GoTo(int x, int y) {};
  virtual int GetX() const {};
  virtual int GetY() const {};

};

painter.h

painter.h

#pragma once
#include "turtle.h"
#include <cerrno>

class error_k
{
public:
    error_k() : errnum(EAGAIN){}
    int num() const
    {
        return errnum;
    }
private:
    int errnum;
};

class Painter
{
        Turtle* turtle;
public:
        Painter() : turtle(new Turtle){}
    Turtle* getTurtle(){
        return turtle;
    }
        bool DrawCircle(int, int, int){
        int rep = 10;
        int ret = 0;
        do{
            try{
                std::cout << "calling PenDown() : rep = " <<
                    rep << " , ret = " << ret << std::endl;
                        ret = turtle->PenDown();
                if(ret > 0)
                    return true;
            }catch(error_k err )
            {
                std::cout << "Caught Exception (" <<
                    err.num() << "): rep = " <<
                    rep << " , ret = " << ret << std::endl;

                delete turtle;
                turtle = new Turtle();
            }
        }while(rep-- > 0 && ret == 0); 
                return false;
        }
    ~Painter(){
        delete turtle;
    }
};

CmakeLists.txt

CmakeLists.txt

cmake_minimum_required(VERSION 2.6)

# Locate GTest
find_package(GTest REQUIRED)
find_package(PkgConfig)
message(STATUS "gtest found: " ${GTEST_FOUND})
pkg_check_modules(GMOCK "gmock" REQUIRED)
message(STATUS "gmock found: " ${GMOCK_FOUND})

include_directories(${GTEST_INCLUDE_DIRS})

# Link runTests with what we want to test and the GTest and pthread library
add_executable(runTests test/mock_turtle_test.cc)
target_link_libraries(runTests ${GTEST_LIBRARIES} ${GMOCK_LIBRARIES} gmock gmock_main gtest gtest_main)
target_include_directories(runTests SYSTEM PRIVATE inc)
add_test(NAME runTests COMMAND runTests)
enable_testing()

输出:

$ ./runTests 
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from PainterTest
[ RUN      ] PainterTest.CanDrawSomething
calling PenDown() : rep = 10 , ret = 0
Caught Exception (11): rep = 10 , ret = 0
calling PenDown() : rep = 9 , ret = 0

GMOCK WARNING:
Uninteresting mock function call - returning default value.
    Function call: PenDown()
          Returns: 0
NOTE: You can safely ignore the above warning unless this call should not happen.  Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call.  See https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#knowing-when-to-expect for details.
calling PenDown() : rep = 8 , ret = 0

GMOCK WARNING:
Uninteresting mock function call - returning default value.
    Function call: PenDown()
          Returns: 0
NOTE: You can safely ignore the above warning unless this call should not happen.  Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call.  See https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#knowing-when-to-expect for details.
calling PenDown() : rep = 7 , ret = 0

GMOCK WARNING:
Uninteresting mock function call - returning default value.
    Function call: PenDown()
          Returns: 0
NOTE: You can safely ignore the above warning unless this call should not happen.  Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call.  See https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#knowing-when-to-expect for details.
calling PenDown() : rep = 6 , ret = 0

GMOCK WARNING:
Uninteresting mock function call - returning default value.
    Function call: PenDown()
          Returns: 0
NOTE: You can safely ignore the above warning unless this call should not happen.  Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call.  See https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#knowing-when-to-expect for details.
calling PenDown() : rep = 5 , ret = 0

GMOCK WARNING:
Uninteresting mock function call - returning default value.
    Function call: PenDown()
          Returns: 0
NOTE: You can safely ignore the above warning unless this call should not happen.  Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call.  See https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#knowing-when-to-expect for details.
calling PenDown() : rep = 4 , ret = 0

GMOCK WARNING:
Uninteresting mock function call - returning default value.
    Function call: PenDown()
          Returns: 0
NOTE: You can safely ignore the above warning unless this call should not happen.  Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call.  See https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#knowing-when-to-expect for details.
calling PenDown() : rep = 3 , ret = 0

GMOCK WARNING:
Uninteresting mock function call - returning default value.
    Function call: PenDown()
          Returns: 0
NOTE: You can safely ignore the above warning unless this call should not happen.  Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call.  See https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#knowing-when-to-expect for details.
calling PenDown() : rep = 2 , ret = 0

GMOCK WARNING:
Uninteresting mock function call - returning default value.
    Function call: PenDown()
          Returns: 0
NOTE: You can safely ignore the above warning unless this call should not happen.  Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call.  See https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#knowing-when-to-expect for details.
calling PenDown() : rep = 1 , ret = 0

GMOCK WARNING:
Uninteresting mock function call - returning default value.
    Function call: PenDown()
          Returns: 0
NOTE: You can safely ignore the above warning unless this call should not happen.  Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call.  See https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#knowing-when-to-expect for details.
calling PenDown() : rep = 0 , ret = 0

GMOCK WARNING:
Uninteresting mock function call - returning default value.
    Function call: PenDown()
          Returns: 0
NOTE: You can safely ignore the above warning unless this call should not happen.  Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call.  See https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#knowing-when-to-expect for details.
/home/preetam/Desktop/gmock/cpp_mock_ptr/test/mock_turtle_test.cc:18: Failure
Value of: painter.DrawCircle(0, 0, 10)
  Actual: false
Expected: true
[  FAILED  ] PainterTest.CanDrawSomething (1 ms)
[----------] 1 test from PainterTest (1 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (1 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] PainterTest.CanDrawSomething

 1 FAILED TEST

推荐答案

您在原始乌龟模拟游戏中定义了期望,但是在第一个异常之后,您删除并重新创建了该模拟游戏.在那之后,新的模拟没有定义任何期望,因此对它的所有调用都被报告为无趣的.

You define the expectation on the original turtle mock, but after the first exception you delete and re-create the mock. After that, the new mock has no expectations defined on it and as such all calls to it are reported as uninteresting.

这篇关于gmock/google-mock发出警告并通过模拟异常使测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆