Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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++ 在C++;_C++_Unit Testing_Googletest - Fatal编程技术网

C++ 在C++;

C++ 在C++;,c++,unit-testing,googletest,C++,Unit Testing,Googletest,我已经编写了以下代码: //Source code sample //Class A is the class under test class A{ public: A(){ B *b = new B; } void func1() { b->func2(); } }; class B { void func2() { //here a function from third class is called. } }; //T

我已经编写了以下代码:

//Source code sample

//Class A is the class under test
class A{
public:
  A(){
    B *b = new B;
  }
  void func1()
  {
    b->func2();
  }
};

class B
{
  void func2()
  {
    //here a function from third class is called.
  }
};

//Test case
class MockB : public B
{
  MOCK_METHOD0(func2(), void());
}

TEST_F(A, TC1_func1)
{
  MockB mock;
  A *a = new A;
  a->func1();
  EXPECT_CALL(mock, func2()).Times(1);
}
这里func1的主体只包含对func2的调用。我想为func1编写一个测试用例。 我的问题是,我们如何测试func1是否调用了func2()

注:

  • 类A创建类B的实例
  • 我不想将类B的指针作为参数传递给类A的构造函数
  • 我不想对源代码做任何更改

  • 提前感谢。

    “我不想将类B的指针作为参数传递给类A的构造函数”,但这确实是答案。这是如何正确地组合类型以允许良好的模拟和单元测试。你为什么不想做呢?“我不想对源代码做任何更改。”。悲哀的是,它没有编译:
    b
    不是一个成员,而是一个局部变量。所有的
    new
    泄漏可能会被避免。1。我不想将类B的指针作为参数传递给类A的构造函数。这是因为,代码在开发中已经取得了很大进展。此时对源代码的任何更改都可能导致缺陷。更新测试以使代码更清晰。对象b是类a的成员,对象mock是测试F的成员。在func1()中,对对象b调用func2。但是在TEST_F中,如何确保func2被成功调用?Func2既不接受任何参数,也不返回任何值。任何建议/解决方案都会大有裨益。“我不想把B类的指针作为参数传递给A类的构造函数”,但这确实是答案。这是如何正确地组合类型以允许良好的模拟和单元测试。你为什么不想做呢?“我不想对源代码做任何更改。”。悲哀的是,它没有编译:
    b
    不是一个成员,而是一个局部变量。所有的
    new
    泄漏可能会被避免。1。我不想将类B的指针作为参数传递给类A的构造函数。这是因为,代码在开发中已经取得了很大进展。此时对源代码的任何更改都可能导致缺陷。更新测试以使代码更清晰。对象b是类a的成员,对象mock是测试F的成员。在func1()中,对对象b调用func2。但是在TEST_F中,如何确保func2被成功调用?Func2既不接受任何参数,也不返回任何值。任何建议/解决方案都会大有帮助。