UnitTest Fake
什麼是Fack
- Fake Function Framework ,是一種實體的模擬。
- Fake是一種虛假的實現,可以自定義返回的結果。
- 簡單理解的話,Fake用於C函數。用來模擬函數的返回值。
- 個人理解,Mock用於C++(對象),Fack用於C(函數)
例子
- 相關頭文件,在fake庫中提供,自行搜索下載吧。
- myFake.h
#ifndef MYFACK_H
#define MYFACK_H
#ifndef __cplusplus
# error ERROR: This file requires C++ compilation (use a .cpp suffix)
#endif
#include <fff.h>
#include <fffext.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
DECLARE_FAKE_VALUE_FUNC3(int, socket, int, int, int);
DECLARE_FAKE_VALUE_FUNC3(int, bind, int, const sockaddr*, socklen_t);
DECLARE_FAKE_VALUE_FUNC3(int, connect, int, const sockaddr*, socklen_t);
DECLARE_FAKE_VALUE_FUNC1(int, close, int);
#endif
#include "myFake.h"
DEFINE_FFF_GLOBALS;
DEFINE_FAKE_VALUE_FUNC1(void*, malloc, size_t);
DEFINE_FAKE_VALUE_FUNC3(int, socket, int, int, int);
DEFINE_FAKE_VALUE_FUNC3(int, bind, int, const sockaddr*, socklen_t);
DEFINE_FAKE_VALUE_FUNC3(int, connect, int, const sockaddr*, socklen_t);
DEFINE_FAKE_VALUE_FUNC1(int, close, int);
TEST_F(TestXXX, testSocket)
{
FUNC_FAKE_SINGRET_SET(socket, -1);
FUNC_FAKE_RESET1(socket);
}
TEST_F(TestXXX, testBind)
{
FUNC_FAKE_SINGRET_SET(socket, 10);
FUNC_FAKE_SINGRET_SET(bind, -1);
FUNC_FAKE_RESET2(socket, bind);
}