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>();
    WHEN_CALLED(fakeFileStream->is_open()).Return(true);

    // Act
    CTouchFile touchFile;
    touchFile.Touch("somefile.dll");

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

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 chengwei (600 points)
edited by chengwei

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: http://www.typemock.com/docs/?book=Ipp&page=known_issues.htm

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 David (1.9k 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
{
    public:
        virtual BOOL Touch(const char* aFilePath);
};

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

    if ( inFile.is_open() )
    {
        inFile.close();
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

Hey, Chengwei

Just making sure you are now able to use the ticket portal, I assume there was an issue with the system, have you received our replies?
...