小编Dnj*_*Abc的帖子

GMock:如何将模拟类变量作为返回值返回

我正在尝试首次使用GMock(适用于c ++的Google模拟框架)。我有以下课程:

class LocalCache
{
public:
  virtual time_t GetCurrentTime() = 0;
  virtual int AddEntry(const std::string key, std::string& value);
  virtual int GetEntry(const std::string key, std::string& value);
};
Run Code Online (Sandbox Code Playgroud)

GetEntry方法调用GetCurrentTime调用。我想模拟GetCurrentTime方法,以便提前测试时钟以测试在GetEntry调用过程中发生的条目老化(请不要问我为什么在GetEntry调用过程中进行老化)...这是另一个讨论:()。这是我的模拟类:

class MockLocalCache : public LocalCache
{
public:
  using LocalCache::GetCurrentTime;
  MOCK_METHOD0(GetCurrentTime, time_t());

  MockLocalCache()
  : mCurrentTime(0)
  {
  }

  void EnableFakeTime()
  {
    ON_CALL(*this, GetCurrentTime()).WillByDefault(Return(mCurrentTime));
  }

  void SetTime(time_t now) { mCurrentTime = now; }

private:
  time_t  mCurrentTime;
};

TEST(MockTest, TimeTest)
{
  MockLocalCache mockCache;

  mockCache.EnableFakeTime();

  std::string key("mykey");
  std::string value("My Value"); …
Run Code Online (Sandbox Code Playgroud)

c++ unit-testing googletest googlemock gmock

6
推荐指数
2
解决办法
8508
查看次数

inotify_add_watch 因没有此类文件或目录而失败

我正在尝试在我的 c/c++ 程序中监视文件的创建。我正在尝试使用 inotify 来实现此目的。但是,当我在代码中no such file or directory进行inotify_add_watch()调用时,我收到了。我正在 Ubuntu 16.04 机器上运行我的程序。该机器在 EC2 云中运行。有人可以告诉我收到 的可能原因no such file or directory error吗?

根据inotify_add_watch的手册页,这甚至不是可能的错误代码之一。我已确保我对要监视的文件具有适当的读取权限等。

这是我的测试程序:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/inotify.h>
#include <limits.h>

#define MAX_EVENTS  1024
#define LEN_NAME    16
#define EVENT_SIZE  (sizeof (struct inotify_event))
#define BUF_LEN     (MAX_EVENTS * (EVENT_SIZE + LEN_NAME))

int
main(int argc, char **argv)
{
  int length, i = 0, wd;
  int fd;
  char buffer[BUF_LEN];

  /* Initialize …
Run Code Online (Sandbox Code Playgroud)

c c++ amazon-ec2 inotify ubuntu-16.04

5
推荐指数
1
解决办法
5613
查看次数