EXPECT_CALL not happening in File I/O

363 views
Skip to first unread message

writeint...@gmail.com

unread,
Jan 19, 2016, 8:49:33 PM1/19/16
to Google C++ Mocking Framework
Hi ,
I was trying this example and cannot figure out the problem with the failure.
This is simple file interface i am having and the test  failing when i provide the parameter to the EXPECT_CALL,
But when I have the expect call in the below fashion - the test passes


    EXPECT_CALL(fio, Open(_,_))
      .WillOnce(Return(test_file));
    EXPECT_CALL(fio, Write(_, _, _, test_file))
        .WillOnce(Return(sizeof(data)));
    EXPECT_CALL(fio, Close(test_file))
      .WillOnce(Return(0));


The error scenario code is pasted below and also the error i get when run the exe.

Any help is appreciated.

//Interface
#include <cstdio>
#include <cstddef>

class IFileInputOutput {
public:
  virtual ~IFileInputOutput() {}
  virtual FILE* Open(const char* filename, const char* mode) = 0;
  virtual size_t Write(const void* data, size_t size, size_t num, FILE* file) = 0;
  virtual int Close(FILE* file) = 0;
};


//Implementation
#include <cstdio>
#include "IFileInputOutput.h"

class ImplFileInputOutput : public IFileInputOutput {
public:

  ImplFileInputOutput() {}
  virtual ~ImplFileInputOutput() {}

  virtual FILE* Open(const char* filename, const char* mode) {
      return fopen(filename, mode);
  }
  virtual size_t Write(const void* data, size_t size, size_t num, FILE* file) {
      return fwrite(data, size, num, file);
  }
  virtual int Close(FILE* file) {
      return fclose(file);
  }
};

//class under test Writer.h
#include "IFileInputOutput.h"
class Writer {
public:
    Writer(IFileInputOutput &objfile);
    virtual ~Writer();
    bool WriteBlog(const char* filename, const void* data, size_t size);
    //void WriteNovel();

private:
    IFileInputOutput &objFileInputOutput;
};

//Writer.cpp
#include "Writer.h"



Writer::Writer(IFileInputOutput &objfile):objFileInputOutput(objfile) { }

Writer::~Writer() { }

// Writes a file, returns true on success.
bool Writer::WriteBlog(const char* filename, const void* data, size_t size) {

   FILE* file = objFileInputOutput.Open(filename, "w+");
   
   if (!file) {
     return false;
   }

   if (objFileInputOutput.Write(data, 1, size, file) != size) {  
     return false;
   }

   if (objFileInputOutput.Close(file) != 0) {
     return false;
   }
   return true;
}

//Mock class
#include <cstddef>
#include <cstdio>
#include <gmock/gmock.h>
#include "IFileInputOutput.h"

class FileInputOutputMock : public IFileInputOutput {
public:

  MOCK_METHOD2(Open, FILE*(const char * filename, const char* mode));
  MOCK_METHOD4(Write, size_t(const void* data, size_t size, size_t num, FILE* file));
  MOCK_METHOD1(Close, int(FILE* file));
};

//Tests
class FileInputOutputTest : public ::testing::Test
{
public:
    void SetUp(){};
    void TearDown(){};

    FileInputOutputTest(){};
    virtual ~FileInputOutputTest(){};
};

TEST_F(FileInputOutputTest, SuccessWorks) {

   FileInputOutputMock fio;
   Writer _objWriter(fio);

  static char data[] = "hello";
  const char* kName = "test.txt";

  FILE* test_file;

    EXPECT_CALL(fio, Open(kName, "w+"))
      .WillOnce(Return(test_file));
    EXPECT_CALL(fio, Write(&data, 1, sizeof(data), test_file))
        .WillOnce(Return(sizeof(data)));
    EXPECT_CALL(fio, Close(test_file))
      .WillOnce(Return(0));

  bool ret = _objWriter.WriteBlog(kName, &data, sizeof(data));

  EXPECT_EQ(true, ret);
}



//Error

 * [----------] 1 test from FileInputOutputTest
[ RUN      ] FileInputOutputTest.SuccessWorks
unknown file: Failure

Unexpected mock function call - returning default value.
    Function call: Open(0x47a829 pointing to "test.txt", 0x47ec23 pointing to "w+")
          Returns: NULL
Google Mock tried the following 1 expectation, but it didn't match:

../test/Unit/FileInputOutputTest.cpp:58: EXPECT_CALL(fio, Open(kName, "w+"))...
  Expected arg #1: is equal to 0x47a832 pointing to "w+"
           Actual: 0x47ec23 pointing to "w+"
         Expected: to be called once
           Actual: never called - unsatisfied and active
../test/Unit/FileInputOutputTest.cpp:77: Failure
Value of: ret
  Actual: false
Expected: true
../test/Unit/FileInputOutputTest.cpp:62: Failure
Actual function call count doesn't match EXPECT_CALL(fio, Close(test_file))...
         Expected: to be called once
           Actual: never called - unsatisfied and active
../test/Unit/FileInputOutputTest.cpp:60: Failure
Actual function call count doesn't match EXPECT_CALL(fio, Write(&data, 1, sizeof(data), test_file))...
         Expected: to be called once
           Actual: never called - unsatisfied and active
../test/Unit/FileInputOutputTest.cpp:58: Failure
Actual function call count doesn't match EXPECT_CALL(fio, Open(kName, "w+"))...
         Expected: to be called once
           Actual: never called - unsatisfied and active
[  FAILED  ] FileInputOutputTest.SuccessWorks (1 ms)




Corey Kosak

unread,
Jan 19, 2016, 9:01:58 PM1/19/16
to writeint...@gmail.com, Google C++ Mocking Framework
Hm. Your test actually passes for me, once I give test_file a value (you leave it undefined)

 FILE* test_file = ....put_something_here.....


--

---
You received this message because you are subscribed to the Google Groups "Google C++ Mocking Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googlemock+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/googlemock/cfce7960-2396-4752-8a76-40ca94294a33%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages