找不到 gmock ElementsAreArray() 函数
gmock ElementsAreArray() function not found
我正在尝试在另一个函数中调用函数 ElementsAreArray()。我通常在我的测试中使用它,但我想让测试报告更好,并将其组织在一个函数上。所以我创建了以下文件:
gUnitTest_Test_Array.cpp 我有我的函数定义:
#include "gUnitTest_Test_Array.h"
void gtest_array::expect_that_size1(volatile signed long arrayA[CIC8_TABLE_SIZE],volatile signed long arrayB[CIC8_TABLE_SIZE], std::string msg1, std::string msg2)
{
using namespace testing;
bool HasError = false;
EXPECT_THAT(arrayA, ElementsAreArray(arrayB)) << (HasError = true);
if (HasError)
std::cout << msg1;
else
std::cout << msg2;
}
gUnitTest_Test_Array.h 我有我的 class 声明:
#pragma once
#ifndef GUNITTEST_TEST_ARRAY_H
#define GUNITTEST_TEST_ARRAY_H
#include "gUnitTest_Common_Defs.h"
using namespace std;
class gtest_array
{
public:
void expect_that_size1(volatile signed long arrayA[CIC8_TABLE_SIZE], volatile signed long arrayB[CIC8_TABLE_SIZE], std::string msg1, std::string msg2);
};
#endif
gUnitTest_Common_Defs.h 其中包含必要的库和定义
#pragma once
//#ifndef GUNITTEST_COMMON_DEFS_H
//#define GUNITTEST_COMMON_DEFS_H
#include <stdio.h>
#include <string>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#define ... //Whatever I need to define
然后在我的测试文件中调用函数:
std::string msg1 = "failed";
std::string msg2 = "passed";
TestArray1.expect_that_size1(sG_Demodulation_Diagnostics_Buffers.WNLCPF_CIC8_Decim, s32L_CIC8_table, msg1, msg2);
问题是当我尝试编译时,编译器抱怨它没有找到函数 ElemetsAreArray()。这对我来说没有意义,因为我包含了所有必要的 gtest headers 并且在移动到函数之前,我在我的测试中内联使用了这个函数,如下所示:
EXPECT_THAT(sG_Demodulation_Diagnostics_Buffers.WNLCPF_CIC8_Decim, ElementsAreArray(s32L_CIC8_table)) << "The CIC8 block test failed. Failed at index: " << i;
这是错误信息
Error C2672 'testing::ElementsAreArray': no matching overloaded
function found GUnitTesting \GUnitTesting\gUnitTest_Test_Array.cpp 15
Error C2893 Failed to specialize function template
'testing::internal::ElementsAreArrayMatcherContainer::value_type
testing::ElementsAreArray(const Container
&)' GUnitTesting \GUnitTesting\gUnitTest_Test_Array.cpp 15
Error C2784 'testing::internal::ElementsAreArrayMatcher
testing::ElementsAreArray(const T (&)[N])': could not deduce template
argument for 'const T (&)[N]' from 'volatile long
[]' GUnitTesting \GUnitTesting\gUnitTest_Test_Array.cpp 15
Error C2780 'testing::internal::ElementsAreArrayMatcher
testing::ElementsAreArray(const T *,size_t)': expects 2 arguments - 1
provided GUnitTesting \GUnitTesting\gUnitTest_Test_Array.cpp 15
Error C2780 'testing::internal::ElementsAreArrayMatcher<::std::iterator_traits<_Ty>::value_type>
testing::ElementsAreArray(Iter,Iter)': expects 2 arguments - 1
provided GUnitTesting \GUnitTesting\gUnitTest_Test_Array.cpp 15
Error C2672 'testing::internal::MakePredicateFormatterFromMatcher': no
matching overloaded function
found GUnitTesting \GUnitTesting\gUnitTest_Test_Array.cpp 15
Error C2512 'testing::AssertionResult': no appropriate default
constructor
available GUnitTesting \GUnitTesting\gUnitTest_Test_Array.cpp 15
上面关于 arrayA 和 arrayB 本身不是数组的评论是正确的。我知道,但我认为 ElementsAreArray() 会很高兴从指针接收地址。
结果,正如上面所评论的那样,它将它转换为 std::vectors,所以我找到的解决方法是在函数内部创建我的向量的本地副本(如果有人有任何更有效的想法,请告诉我):
#pragma once
//#ifndef GUNITTEST_TEST_ARRAY_H
//#define GUNITTEST_TEST_ARRAY_H
#include "gUnitTest_Common_Defs.h"
using namespace std;
class gtest_array
{
public:
void expect_that_size1(volatile signed long (*arrayA), volatile signed long (*arrayB), uint16_t table_size, std::string msg1, std::string msg2);
};
//#endif
#include "gUnitTest_Test_Array.h"
void gtest_array::expect_that_size1(volatile signed long (*arrayA),volatile signed long (*arrayB), uint16_t table_size, std::string msg1, std::string msg2)
{
using namespace testing;
signed long array1[CIC8_TABLE_SIZE];
signed long array2[CIC8_TABLE_SIZE];
std::copy(arrayA, arrayA + table_size, array1);
std::copy(arrayB, arrayB + table_size, array2);
bool HasError = false;
EXPECT_THAT(array1, ElementsAreArray(array2)) << (HasError = true);
if (HasError)
std::cout << msg1;
else
std::cout << msg2;
}
我正在尝试在另一个函数中调用函数 ElementsAreArray()。我通常在我的测试中使用它,但我想让测试报告更好,并将其组织在一个函数上。所以我创建了以下文件:
gUnitTest_Test_Array.cpp 我有我的函数定义:
#include "gUnitTest_Test_Array.h"
void gtest_array::expect_that_size1(volatile signed long arrayA[CIC8_TABLE_SIZE],volatile signed long arrayB[CIC8_TABLE_SIZE], std::string msg1, std::string msg2)
{
using namespace testing;
bool HasError = false;
EXPECT_THAT(arrayA, ElementsAreArray(arrayB)) << (HasError = true);
if (HasError)
std::cout << msg1;
else
std::cout << msg2;
}
gUnitTest_Test_Array.h 我有我的 class 声明:
#pragma once
#ifndef GUNITTEST_TEST_ARRAY_H
#define GUNITTEST_TEST_ARRAY_H
#include "gUnitTest_Common_Defs.h"
using namespace std;
class gtest_array
{
public:
void expect_that_size1(volatile signed long arrayA[CIC8_TABLE_SIZE], volatile signed long arrayB[CIC8_TABLE_SIZE], std::string msg1, std::string msg2);
};
#endif
gUnitTest_Common_Defs.h 其中包含必要的库和定义
#pragma once
//#ifndef GUNITTEST_COMMON_DEFS_H
//#define GUNITTEST_COMMON_DEFS_H
#include <stdio.h>
#include <string>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#define ... //Whatever I need to define
然后在我的测试文件中调用函数:
std::string msg1 = "failed";
std::string msg2 = "passed";
TestArray1.expect_that_size1(sG_Demodulation_Diagnostics_Buffers.WNLCPF_CIC8_Decim, s32L_CIC8_table, msg1, msg2);
问题是当我尝试编译时,编译器抱怨它没有找到函数 ElemetsAreArray()。这对我来说没有意义,因为我包含了所有必要的 gtest headers 并且在移动到函数之前,我在我的测试中内联使用了这个函数,如下所示:
EXPECT_THAT(sG_Demodulation_Diagnostics_Buffers.WNLCPF_CIC8_Decim, ElementsAreArray(s32L_CIC8_table)) << "The CIC8 block test failed. Failed at index: " << i;
这是错误信息
Error C2672 'testing::ElementsAreArray': no matching overloaded function found GUnitTesting \GUnitTesting\gUnitTest_Test_Array.cpp 15
Error C2893 Failed to specialize function template 'testing::internal::ElementsAreArrayMatcherContainer::value_type testing::ElementsAreArray(const Container &)' GUnitTesting \GUnitTesting\gUnitTest_Test_Array.cpp 15
Error C2784 'testing::internal::ElementsAreArrayMatcher testing::ElementsAreArray(const T (&)[N])': could not deduce template argument for 'const T (&)[N]' from 'volatile long []' GUnitTesting \GUnitTesting\gUnitTest_Test_Array.cpp 15
Error C2780 'testing::internal::ElementsAreArrayMatcher testing::ElementsAreArray(const T *,size_t)': expects 2 arguments - 1 provided GUnitTesting \GUnitTesting\gUnitTest_Test_Array.cpp 15
Error C2780 'testing::internal::ElementsAreArrayMatcher<::std::iterator_traits<_Ty>::value_type> testing::ElementsAreArray(Iter,Iter)': expects 2 arguments - 1 provided GUnitTesting \GUnitTesting\gUnitTest_Test_Array.cpp 15
Error C2672 'testing::internal::MakePredicateFormatterFromMatcher': no matching overloaded function found GUnitTesting \GUnitTesting\gUnitTest_Test_Array.cpp 15
Error C2512 'testing::AssertionResult': no appropriate default constructor available GUnitTesting \GUnitTesting\gUnitTest_Test_Array.cpp 15
上面关于 arrayA 和 arrayB 本身不是数组的评论是正确的。我知道,但我认为 ElementsAreArray() 会很高兴从指针接收地址。 结果,正如上面所评论的那样,它将它转换为 std::vectors,所以我找到的解决方法是在函数内部创建我的向量的本地副本(如果有人有任何更有效的想法,请告诉我):
#pragma once
//#ifndef GUNITTEST_TEST_ARRAY_H
//#define GUNITTEST_TEST_ARRAY_H
#include "gUnitTest_Common_Defs.h"
using namespace std;
class gtest_array
{
public:
void expect_that_size1(volatile signed long (*arrayA), volatile signed long (*arrayB), uint16_t table_size, std::string msg1, std::string msg2);
};
//#endif
#include "gUnitTest_Test_Array.h"
void gtest_array::expect_that_size1(volatile signed long (*arrayA),volatile signed long (*arrayB), uint16_t table_size, std::string msg1, std::string msg2)
{
using namespace testing;
signed long array1[CIC8_TABLE_SIZE];
signed long array2[CIC8_TABLE_SIZE];
std::copy(arrayA, arrayA + table_size, array1);
std::copy(arrayB, arrayB + table_size, array2);
bool HasError = false;
EXPECT_THAT(array1, ElementsAreArray(array2)) << (HasError = true);
if (HasError)
std::cout << msg1;
else
std::cout << msg2;
}