GMock:EXPECT_CALL 未按预期为文件调用 *
GMock: EXPECT_CALL not called as expected for File *
我正在尝试这个例子,except_call 不满足下面的要求,但如果我不给出任何参数,同样的事情会起作用 -
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));
感谢任何帮助,谢谢!!
我已经发布了 "error message i am getting below"
这是完整的代码。
//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) {
//if (obj.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)
这个错误的原因是函数 Open
的参数是 char *
类型,所以当
您指定要匹配的参数,您实际上是在请求匹配指针。自从
您已经在测试主体中创建了第一个请求的参数 kName
并将其传递给
函数,该指针已正确匹配(请注意,尽管此检查通过,但未通过
匹配字符串文字的正确方法)。但是,当匹配第二个参数时,你是
实际上匹配测试主体中定义的字符串文字的第一个元素的地址和另一个
一个在函数内部定义的。尽管它们指向相同的字符串文字,但它们是
两个不同的地址,这就是匹配失败的原因。
要正确匹配字符串文字,请使用 gmock
中的 StrEq
匹配器。以下是您的操作方法:
EXPECT_CALL(fio, Open(StrEq(kName), StrEq("w+")))
.WillOnce(Return(test_file));
我正在尝试这个例子,except_call 不满足下面的要求,但如果我不给出任何参数,同样的事情会起作用 -
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));
感谢任何帮助,谢谢!! 我已经发布了 "error message i am getting below"
这是完整的代码。
//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) {
//if (obj.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)
这个错误的原因是函数 Open
的参数是 char *
类型,所以当
您指定要匹配的参数,您实际上是在请求匹配指针。自从
您已经在测试主体中创建了第一个请求的参数 kName
并将其传递给
函数,该指针已正确匹配(请注意,尽管此检查通过,但未通过
匹配字符串文字的正确方法)。但是,当匹配第二个参数时,你是
实际上匹配测试主体中定义的字符串文字的第一个元素的地址和另一个
一个在函数内部定义的。尽管它们指向相同的字符串文字,但它们是
两个不同的地址,这就是匹配失败的原因。
要正确匹配字符串文字,请使用 gmock
中的 StrEq
匹配器。以下是您的操作方法:
EXPECT_CALL(fio, Open(StrEq(kName), StrEq("w+")))
.WillOnce(Return(test_file));