14
#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
一起指定時纔有意思?
使用':: testing :: nice_mock模擬;'爲了避免這些警告對於模擬上未明確預期的調用。 –
2014-10-02 08:35:12
也許[這將幫助](https://code.google.com/p/googlemock/wiki/FrequentlyAskedQuestions#I_got_a_warning_「Uninteresting_function_call_encountered_-) – 2014-10-02 08:44:33
Doh!我只查了gmock食譜和cheatsheet :( – Bulletmagnet 2014-10-02 08:50:16