代码之家  ›  专栏  ›  技术社区  ›  TheProgrammingBatman

Google Test WillOnce(Return())操作预期的返回值

  •  0
  • TheProgrammingBatman  · 技术社区  · 8 年前

    我目前正在尝试用c++测试google test的功能。(更具体地说:google mock)

    现在我遇到了一个问题。这应该是非常容易的,它以前工作过,但不知怎么搞砸了,框架没有按预期工作。够了,这是我的问题。

    我试着做一件简单的事情,看看函数“DoSomeMathTurtle”是否被调用一次并返回期望值。但是“.WillOnce(Return(x))”操纵期望值,使其始终为真。

    class Turtle {
        ...
        virtual int DoSomeMathTurtle(int , int); //Adds two ints together and returns them
        ...
    };
    

    我的嘲笑班:

        class MockTurtle : public Turtle{
        public:
            MockTurtle(){
                ON_CALL(*this, DoSomeMathTurtle(_, _))
                    .WillByDefault(Invoke(&real_, Turtle::DoSomeMathTurtle)); 
                //I did this so the function gets redirected to a real
                // object so those two numbers actually get added together 
                //(without that it returns always 0)
    
            }
            MOCK_METHOD2(DoSomeMathTurtle, int(int, int));
        private:
            Turtle real_;
        };
    

    基本测试等级:

    class TestTurtle : public ::testing::Test{
    protected:
        //De- & Constructor
        TestTurtle(){}
        virtual ~TestTurtle(){} //Has to be virtual or all tests will fail
    
        //Code executed before and after every test
        virtual void SetUp(){}
        virtual void TearDown(){}
    
        //Test Fixtures which can be used in each test
        MockTurtle m_turtle;
     };
    

        TEST_F(TestTurtle, MathTest){
                int x = 2; // Expected value
                int rvalue; // Returned value of the function
    
                EXPECT_CALL(m_turtle, DoSomeMathTurtle(_, _))
                    .Times(1)
                    .WillOnce(Return(x));
    
                    rvalue = m_turtle.DoSomeMathTurtle(3,3); // rvalue should be 6 but it is 2
                    cout << "[          ] Expected value: " << x << " Returned value " << rvalue << endl;
                    //This prints: [          ] Expected value 2 Returned value 2
                    //Then the test passes !?
                }
            }
    

    当我评论 "WillOnce(Return(x))" 出,rvalue变成6(它应该变成的值)。什么时候? “WillOnce(返回(x))” &安培;

    ON_CALL(*this, DoSomeMathTurtle(_, _))
                    .WillByDefault(Invoke(&real_, Turtle::DoSomeMathTurtle)); 
    

    所以只要 “WillOnce(返回(x))” "x" .

    提前谢谢你的帮助! 祝你有一个愉快的,没有虫子的一天!

    1 回复  |  直到 8 年前
        1
  •  1
  •   Michał Kalinowski    8 年前

    嘲笑就是期望。当你在测试中写作时:

    EXPECT_CALL(m_turtle, DoSomeMathTurtle(_, _))
      .Times(1)
      .WillOnce(Return(x));
    

    你期待的 m_turtle 会打一次电话 DoSomeMathTurtle 任何争论都会有一次 return x .

    因为 x m_turtle.DoSomeMathTurtle(any_argument, any_argument); 它将返回2。

    如果你想检查参数,你应该在googlemock,f.e中寻找匹配者

    EXPECT_CALL(m_turtle, DoSomeMathTurtle(Eq(3), Lt(3)))
    

    你会期待的 当第一个等于3,第二个小于3时,将接受两个参数。

    EXPECT_CALL(m_turtle, DoSomeMathTurtle(Eq(3), Eq(3)))
      .Times(1)
      .WillOnce(Return(6));
      rvalue = m_turtle.DoSomeMathTurtle(3,3); // rvalue is equal to 6