2014-10-02 91 views
14

已知:有没有办法让ON_CALL模拟功能“有趣”?

#include "gmock/gmock.h" 
#include <string> 

using namespace testing; // tsk, tsk 

// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 

struct Mockable { 
    virtual std::string ify(int x) const; 
}; 

std::string Mockable::ify(int x) const 
{ 
    return std::to_string(x); 
} 

// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 

struct Mocked : public Mockable { 
    MOCK_CONST_METHOD1(ify, std::string(int)); 
}; 

// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 

std::string tested(Mockable const& u, int x) 
{ 
    return u.ify(x); 
} 

TEST(TC,T42) 
{ 
    Mocked mock; 
    ON_CALL(mock, ify(Eq(42))) 
    .WillByDefault(Return("33")); 

    std::string const& ret = tested(mock, 42); 
    EXPECT_EQ("42", ret); 
} 

TEST(TC,T33) 
{ 
    Mocked mock; 
    ON_CALL(mock, ify(Eq(33))) 
    .WillByDefault(Return("333")); 

    std::string const& ret = tested(mock, 42); 
    EXPECT_EQ("42", ret); 
} 

int main(int argc, char *argv[]) 
{ 
    ::testing::InitGoogleTest(&argc, argv); 
    //::testing::FLAGS_gmock_verbose = "info"; 

    return RUN_ALL_TESTS(); 
} 

的输出是:

$ ./mocktest 
[==========] Running 2 tests from 1 test case. 
[----------] Global test environment set-up. 
[----------] 2 tests from TC 
[ RUN  ] TC.T42 

GMOCK WARNING: 
Uninteresting mock function call - taking default action specified at: 
mocktest.cc:40: 
    Function call: ify(42) 
      Returns: "33" 
Stack trace: 
mocktest.cc:44: Failure 
Value of: ret 
    Actual: "33" 
Expected: "42" 
[ FAILED ] TC.T42 (0 ms) 
[ RUN  ] TC.T33 

GMOCK WARNING: 
Uninteresting mock function call - returning default value. 
    Function call: ify(42) 
      Returns: "" 
Stack trace: 
mocktest.cc:54: Failure 
Value of: ret 
    Actual: "" 
Expected: "42" 
[ FAILED ] TC.T33 (1 ms) 
[----------] 2 tests from TC (1 ms total) 

[----------] Global test environment tear-down 
[==========] 2 tests from 1 test case ran. (1 ms total) 
[ PASSED ] 0 tests. 
[ FAILED ] 2 tests, listed below: 
[ FAILED ] TC.T42 
[ FAILED ] TC.T33 

在两种情况下,Gmock行为正确w.r.t.应用行为(或不在TC33中)。但是为什么在这两种情况下都会说“无趣的模拟函数调用”?模拟函数调用只有在与EXPECT_CALL一起指定时才有意思?

+0

使用':: testing :: nice_mock 模拟;'为了避免这些警告对于模拟上未明确预期的调用。 – 2014-10-02 08:35:12

+1

也许[这将帮助](https://code.google.com/p/googlemock/wiki/FrequentlyAskedQuestions#I_got_a_warning_“Uninteresting_function_call_encountered_-) – 2014-10-02 08:44:33

+0

Doh!我只查了gmock食谱和cheatsheet :( – Bulletmagnet 2014-10-02 08:50:16

回答

16

“只有当EXPECT_CALL指定模拟函数调用有意思吗?”

总之,是的。
ON_CALL()宏只影响在模拟方法调用上采取的操作,但不影响在模拟对象上设置的调用期望。

虽然您可以使用NiceMock<>模板来抑制这些警告。
To cite from google mocks "Cookbook"

假设你的测试使用模拟类MockFoo:

TEST(...) { 
    MockFoo mock_foo; 
    EXPECT_CALL(mock_foo, DoThis()); 
    ... code that uses mock_foo ... 
} 

如果mock_foo比DoThis()被调用时,它会通过谷歌模拟报告为其他的方法一个警告。不过,如果你重写你的测试中使用的NiceMock相反,警告将会消失,导致清洁剂测试输出:

using ::testing::NiceMock; 

TEST(...) { 
    NiceMock<MockFoo> mock_foo; 
    EXPECT_CALL(mock_foo, DoThis()); 
    ... code that uses mock_foo ... 
} 

NiceMock<MockFoo>MockFoo一个子类,因此它可以用于任何MockFoo被接受。

相关问题