Я создаю фиктивный класс istream, чтобы протестировать класс.Но фиктивный объект не передается фактическому классу, в котором я хочу использовать.
Это фиктивный класс.
class MockStream{
private:
std::string filename_;
public:
MockStream(){
}
MockStream(std::string filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out):filename_(filename){
}
MockStream(const MockStream &a){}
MockStream& operator=(const MockStream& a){
return *this;
}
~MockStream(){}
MOCK_CONST_METHOD0(is_open,bool());
MOCK_METHOD0(close,void());
MOCK_METHOD2(read,MockStream&(char*,std::streamsize));
MOCK_METHOD2(write,MockStream&(const char*,std::streamsize));
MOCK_METHOD2(seekg,MockStream&(std::streamoff,std::ios_base::seekdir));
MOCK_METHOD1(seekg,MockStream&(std::streampos));
MOCK_METHOD0(get_filename,std::string());
};
test:
TEST(random_access_stack, mocked_test)
{
RandomAccessStack<TestClass,u_int64_t,MockStream> stack;
MockStream mocked;
EXPECT_CALL(mocked,is_open()).Times(1).WillOnce(testing::Return(true));
stack.SetStream(mocked);
EXPECT_TRUE(stack.is_open());
}
SetStreamфункция:
void SetStream(STREAM& handle){
file_handle_ = handle;
}
где file_handle_ имеет тип MockStream, переданный шаблоном
Вывод:
GMOCK WARNING:
Uninteresting mock function call - returning default value.
Function call: is_open()
Returns: false
NOTE: You can safely ignore the above warning unless this call should not happen. Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call. See https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md#knowing-when-to-expect for details.
/build/libs/storage/tests/gtest/random_access_stack_tests.cpp:79: Failure
Value of: stack.is_open()
Actual: false
Expected: true
/build/libs/storage/tests/gtest/random_access_stack_tests.cpp:73: Failure
Actual function call count doesn't match EXPECT_CALL(mocked, is_open())...
Expected: to be called once
Actual: never called - unsatisfied and active