簡體   English   中英

CppUMock 用於 mocking open() function

[英]CppUMock for mocking open() function

是否可以使用 CppUTest 模擬 C open() function?

#include "CppUTest/CommandLineTestRunner.h"
#include "CppUTest/TestHarness.h"
#include "CppUTestExt/MockSupport.h"
#include <fcntl.h>

extern "C"
{
#include "drivers/my_driver.h"
}

TEST_GROUP(Driver)
{
    void teardown() {
        mock().clear();
    }
};

int open(const char *__path, int __oflag, ...)
{
    return int(mock().actualCall(__func__)
        .withParameter("__path", __path)
        .withParameter("__oflag", __oflag)
        .returnIntValue());
}

TEST(Driver, simpleTest)
{
    mock().expectOneCall("open")
          .withParameter("/dev/sys/my_hw", O_RDWR)
          .andReturnValue(1);

    mock().checkExpectations();

    bool r = open_driver();
    CHECK_TRUE(r);
}

int main(int ac, char** av)
{
    return CommandLineTestRunner::RunAllTests(ac, av);
}

這是我的open_driver()函數:

#include "drivers/my_driver.h"
#include <string.h>
#include <fcntl.h>

bool open_driver() {
    int fd_driver = open("/dev/iost/mast", O_RDWR);
    char msg[255] = {0};

    if(fd_driver == -1)
        return false;

    return true;
}

現在,我收到以下錯誤:

/home/.../open_driver.cpp:26: error: Failure in TEST(Driver, simpleTest)
        Mock Failure: Expected call WAS NOT fulfilled.
        EXPECTED calls that WERE NOT fulfilled:
                open -> int /dev/sys/my_hw: <2 (0x2)> (expected 1 call, called 0 times)
        EXPECTED calls that WERE fulfilled:
                <none>

.
Errors (1 failures, 1 tests, 1 ran, 1 checks, 0 ignored, 0 filtered out, 0 ms)

使用編譯器選項 --wrap 來處理這種情況。 有關詳細信息,請參閱以下鏈接: 如何正確使用 `--wrap` 選項包裝函數?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM