chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes

Can Isolator++ fake the std::ifstream class?

The following is my test code. The CTouchFile::Touch() is the function under test which does the following:

1. Accepts a file name parameter
2. Calls std::ifstream::open() to open the file
3. Calls std::ifstream::is_open() to check if the file is opened
4. If std::ifstream::is_open() returns true, then calls std::ifstream::close() to close the file, otherwise not.

The test code is testing \"if the file exists, then both std::ifstream::open() and std::ifstream::close() should be called\". It tries to fake the std::ifstream class and control is_open() return value:

TEST(CTouchFileTests, Touch_FileExists_CallsFileOpenAndClose)
    // Arrange
    std::ifstream *fakeFileStream = FAKE_ALL<std::ifstream>();

    // Act
    CTouchFile touchFile;

    // Assert
    ASSERT_WAS_CALLED(fakeFileStream->open(\"somefile.dll\", _));

The test code compiles without errors. However, when running the test, I got the following exception: \"The class \'std::basic_ifstream<char,struct std::char_traits<char> >\' is an internal system class, hence cannot be faked\".

Is this a known limitation for Isolator++? If yes, is there any other way to fake the std::ifstream class?

asked by (600 points)
edited by

1 Answer

0 votes

Hey Chengwei,

ifstream is part of std library which is not fakeable.

you can see the list of unfakeable libraries here:

You can send a sample of your class that has the method touch to our support team and we will try to find a workaround for that.

answered by (1.7k points)

Thank you, David, for your reference link!

I am not sure if it is because I am a trial user, I seemed to be unable to use the ticket portal. So I will post my class here and I would appreciate if there can be any suggestion for me. Thanks a lot!

class CTouchFile
        virtual BOOL Touch(const char* aFilePath);

BOOL CTouchFile::Touch(const char* aFilePath)
    std::ifstream inFile; aFilePath , std::ios_base::binary );

    if ( inFile.is_open() )
        return TRUE;
        return FALSE;