2014-09-26 83 views
1

也许一个技巧的问题,我的问题是,如果我写的:GoogleMock:如何准确地预期一个呼叫和某个参数,并查看故障诊断?

EXPECT_CALL(mock, handleMessage(_)).Times(0);       // expectation #1 
EXPECT_CALL(mock, handleMessage(Pointee(IsLike(aSpecificMessage)))); // expectation #2 

...和方法handleMessage被调用一次,但用不同的参数(不aSpecificMessage),那么失败的样子:

Mock function called more times than expected - returning default value. 
    Function call: handleMessage(0x8b5378) 
      Returns: false 
     Expected: to be never called 
      Actual: called once - over-saturated and active 

Google Mock不会打印关于为什么参数与期望#2中的谓词不匹配的诊断。 这可能是因为期望#1是第一个失败(?)。

如果我省略预期#1,则故障是冗长的,沿着线:

Google Mock tried the following 1 expectation, but it didn't match: 

../../test/Test.cpp:143: EXPECT_CALL(mock, handleMessage(Pointee(IsLike(aSpecificMessage))))... 
    Expected arg #0: points to a value that <....> 
      Actual: 0xfeaf18, which points to <.....> 

我使用自定义匹配器IsLike我通过产生非常明确的不匹配原因的麻烦去,我想要他们打印。我也不想放弃期望#1,因为它在“默认”部分,默认情况下,我不想在整个测试的其余部分进行模拟调用。

回答

2

看起来你应该使用the StrictMock template modifier。让我们用这个简单的类和模拟:

struct Foo 
{ 
    virtual void function(int) { 
    } 
}; 

struct MockFoo: public Foo 
{ 
    MOCK_METHOD1(function, void(int x)); 
}; 

让我们先从基本的测试,练习这种方法:

TEST(MockTest, basic_one_expectation) 
{ 
    MockFoo foo; 
    EXPECT_CALL(foo, function(4)); 
    Foo& foo1(foo); 
    foo1.function(3); 
} 

输出:

 
[ RUN  ] MockTest.basic_one_expectation 
unknown file: Failure 

Unexpected mock function call - returning directly. 
    Function call: function(3) 
Google Mock tried the following 1 expectation, but it didn't match: 

mock-test.cpp:298: EXPECT_CALL(foo, function(4))... 
    Expected arg #0: is equal to 4 
      Actual: 3 
     Expected: to be called once 
      Actual: never called - unsatisfied and active 
mock-test.cpp:298: Failure 
Actual function call count doesn't match EXPECT_CALL(foo, function(4))... 
     Expected: to be called once 
      Actual: never called - unsatisfied and active 
[ FAILED ] MockTest.basic_one_expectation (1 ms) 

这就是你的选择之一已经考虑过了,但是你不需要它,因为你有其他的测试对函数是否被调用没有任何特别的期望,并且你想让这些测试失败,如果函数变成了c无论如何。作为提醒,让我们来看看发生了什么,当我们试图这样一个测试:

TEST(MockTest, basic_no_expectation) 
{ 
    MockFoo foo; 
    Foo& foo1(foo); 
    foo1.function(3); 
} 

输出:

 
[ RUN  ] MockTest.basic_no_expectation 

GMOCK WARNING: 
Uninteresting mock function call - returning directly. 
    Function call: function(3) 
Stack trace: 
[  OK ] MockTest.basic_no_expectation (1 ms) 

我们得到一个警告,但测试仍然通过。这对你没有好处。让我们来看看什么样的影响StrictMock有:

TEST(MockTest, strict_no_expectation) 
{ 
    ::testing::StrictMock<MockFoo> foo; 
    Foo& foo1(foo); 
    foo1.function(3); 
} 

输出:

 
[ RUN  ] MockTest.strict_no_expectation 
unknown file: Failure 
Uninteresting mock function call - returning directly. 
    Function call: function(3) 
[ FAILED ] MockTest.strict_no_expectation (0 ms) 

我们没有明确地说,我们不想让该函数被调用,但是当函数被称为反正,测试正确失败。正是你想要的。

最后,让我们来看看什么情况下用StrictMock发生在有为函数的参数明确的预期:

TEST(MockTest, strict_one_expectation) 
{ 
    ::testing::StrictMock<MockFoo> foo; 
    EXPECT_CALL(foo, function(4)); 
    Foo& foo1(foo); 
    foo1.function(3); 
} 

输出:

 
[ RUN  ] MockTest.strict_one_expectation 
unknown file: Failure 

Unexpected mock function call - returning directly. 
    Function call: function(3) 
Google Mock tried the following 1 expectation, but it didn't match: 

mock-test.cpp:307: EXPECT_CALL(foo, function(4))... 
    Expected arg #0: is equal to 4 
      Actual: 3 
     Expected: to be called once 
      Actual: never called - unsatisfied and active 
mock-test.cpp:307: Failure 
Actual function call count doesn't match EXPECT_CALL(foo, function(4))... 
     Expected: to be called once 
      Actual: never called - unsatisfied and active 
[ FAILED ] MockTest.strict_one_expectation (0 ms) 

诊断显示的原因参数不匹配,就像上面显示的原始basic_one_expectation测试一样。

+0

哦,是的!就是这样 – haelix 2014-09-26 17:10:10