Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在gmock中使用指向带有EXPECT_调用的模拟对象的指针会导致segfault或默认处理程序启动_C++_Unit Testing_Googletest_Googlemock - Fatal编程技术网

C++ 在gmock中使用指向带有EXPECT_调用的模拟对象的指针会导致segfault或默认处理程序启动

C++ 在gmock中使用指向带有EXPECT_调用的模拟对象的指针会导致segfault或默认处理程序启动,c++,unit-testing,googletest,googlemock,C++,Unit Testing,Googletest,Googlemock,在测试包装器时,我使用gmock模拟底层库。我注意到gmock要求预定义mock对象。我想要的是传递一个指向mock对象的指针。如果动态创建了测试错误处理所需的对象,则该操作不起作用 参考: 代码: $ make ; ./runTests Scanning dependencies of target runTests [ 50%] Building CXX object CMakeFiles/runTests.dir/test/mock_turtle_test.cc.o [100%] Lin

在测试包装器时,我使用gmock模拟底层库。我注意到gmock要求预定义mock对象。我想要的是传递一个指向mock对象的指针。如果动态创建了测试错误处理所需的对象,则该操作不起作用

参考: 代码:

$ make ; ./runTests 
Scanning dependencies of target runTests
[ 50%] Building CXX object CMakeFiles/runTests.dir/test/mock_turtle_test.cc.o
[100%] Linking CXX executable runTests
[100%] Built target runTests
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from PainterTest
[ RUN      ] PainterTest.CanDrawSomething
/home/preetam/Desktop/gmock/cpp_mock_ptr/test/mock_turtle_test.cc:16: Failure
Actual function call count doesn't match EXPECT_CALL(**tt, PenDown())...
         Expected: to be called at least once
           Actual: never called - unsatisfied and active
calling PenDown() : rep = 10 , 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 = 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:20: Failure
Value of: painter.DrawCircle(0, 0, 10)
  Actual: false
Expected: true
[  FAILED  ] PainterTest.CanDrawSomething (0 ms)
[----------] 1 test from PainterTest (0 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
测试/模拟龟测试.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();
}
画家

#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){}
        //Painter(){}
    Turtle** getTurtle(){
        return &turtle;
    }
        bool DrawCircle(int, int, int){
        int rep = 10;
        int ret = 0;
        delete turtle;
        turtle = new Turtle;
        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;
    }
};
输出:

$ make ; ./runTests 
Scanning dependencies of target runTests
[ 50%] Building CXX object CMakeFiles/runTests.dir/test/mock_turtle_test.cc.o
[100%] Linking CXX executable runTests
[100%] Built target runTests
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from PainterTest
[ RUN      ] PainterTest.CanDrawSomething
/home/preetam/Desktop/gmock/cpp_mock_ptr/test/mock_turtle_test.cc:16: Failure
Actual function call count doesn't match EXPECT_CALL(**tt, PenDown())...
         Expected: to be called at least once
           Actual: never called - unsatisfied and active
calling PenDown() : rep = 10 , 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 = 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:20: Failure
Value of: painter.DrawCircle(0, 0, 10)
  Actual: false
Expected: true
[  FAILED  ] PainterTest.CanDrawSomething (0 ms)
[----------] 1 test from PainterTest (0 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

您能否创建一个类型为
FakeTurtle
的模拟对象,并使用依赖项注入技术(查看
Painter
是否需要成为注入对象的所有者;但首先尝试以
Painter
接受对Turtle的引用的方式重构代码,即在其构造函数中使用
Turtle&
,这更简单)

GoogleMock框架要求创建一个模拟对象,并在这个对象(而不是其他对象)上设置所有期望值


旁注:我想让
Turtle
成为一个纯粹的虚拟界面。

你能创建一个类型为
FakeTurtle
的模拟对象并使用依赖注入技术吗(请参见
Painter
是否需要成为注入对象的所有者;但首先尝试以
Painter
接受对Turtle的引用的方式重构代码,即在其构造函数中使用
Turtle&
,这更简单)

GoogleMock框架要求创建一个模拟对象,并在这个对象(而不是其他对象)上设置所有期望值


旁注:我想让
Turtle
成为一个纯虚拟界面。

“我注意到gmock要求预定义模拟对象。我想要的是传递一个指向模拟对象的指针。”但是代码并不是这样的?而且,如果它需要一些东西,那么尊重它可能是一个好主意,因为这可能是您出现异常的原因。我想克服这个限制,使用运行时创建的对象。问题是匹配器。我想知道如何克服它。我仍然没有在您的代码中看到任何与此相关的内容。请在那里发布一个,我在这里添加了完整的代码。我最初提供了另一个问题的链接,该问题有完整的代码。这仍然不是一个最小的可复制示例,而且您仍然有很多代码没有意义,比如painter.h指的是
海龟
,但根据您的代码,它应该只知道
法克图尔
。“我注意到gmock要求预定义mock对象。我想要的是传递一个指向mock对象的指针。"但是代码并不是这样的?而且,如果它需要一些东西,那么尊重它可能是一个好主意,因为这可能是您出现异常的原因。我想克服这个限制,使用运行时创建的对象。问题是匹配器。我想知道如何克服它。我仍然没有在您的代码中看到任何与此相关的内容。请在那里发布一个,我在这里添加了完整的代码。我最初提供了另一个问题的链接,该问题有完整的代码。这仍然不是一个最小的可复制示例,而且您仍然有很多代码没有意义,比如painter.h指的是
海龟
,但根据您的代码,它应该只知道
法克图尔
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()
$ make ; ./runTests 
Scanning dependencies of target runTests
[ 50%] Building CXX object CMakeFiles/runTests.dir/test/mock_turtle_test.cc.o
[100%] Linking CXX executable runTests
[100%] Built target runTests
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from PainterTest
[ RUN      ] PainterTest.CanDrawSomething
/home/preetam/Desktop/gmock/cpp_mock_ptr/test/mock_turtle_test.cc:16: Failure
Actual function call count doesn't match EXPECT_CALL(**tt, PenDown())...
         Expected: to be called at least once
           Actual: never called - unsatisfied and active
calling PenDown() : rep = 10 , 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 = 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:20: Failure
Value of: painter.DrawCircle(0, 0, 10)
  Actual: false
Expected: true
[  FAILED  ] PainterTest.CanDrawSomething (0 ms)
[----------] 1 test from PainterTest (0 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