lundi 26 octobre 2015

How to mock boost throwing an exception?

I have code which interacts with the filesystem using boost which looks like:

FileMigrater::migrate() const {
    //stuff
    try {
        boost::filesystem::create_direcotry(some_path_);
    } catch(const std::exception& e) {
        LOG(ERROR) << "Bad stuff happened";
        return MigrationResult::Failed;
    }
    //more stuff
}

I'm using gmock and gtest to write unit tests for the migrate method and I'd like to write a test for the case where boost throws an exception. Ideally, I'd like to write a unit test which looks something like (the syntax of this will be wrong because I'm new c++ in general):

TEST_F(MyTest, boost_exception_test) {
    ON_CALL(boost_mock, create_directory()).Throw(std::exception);

    EXPECT_EQ(Migration::Failed, migrater.migrate());
}

The problem is that I don't know how to create the boost_mock or even if that's the right approach to the problem.

Aucun commentaire:

Enregistrer un commentaire