Google测试ASSERT_EQ无法编译

时间:2013-08-04 09:01:46

标签: c++ visual-c++ googletest

我有一个类,假设它被命名为Foo,其中我没有定义一个相等运算符,我不想定义一个(出于我自己的原因)。

我想测试一些在Foo上运行的函数,我编写了以下代码:

inline bool operator==(const Foo& left, const Foo& right)
{
   // here  I test my equality condition....
}

TEST(SomeTestCase, SomeTest)
{
    Foo expected = ...
    Foo actual = ...

    ASSERT_EQ(expected, actual);     // does NOT compile
    ASSERT_TRUE(expected == actual); // compiles without a problem
}

有人知道如何编译ASSERT_EQ,以便在出现故障时会打印出有意义的错误信息吗?

我使用的是MSVC2012,错误信息是:

1>D:\3rdpartycache\CPP\gmock\1.6.0-2\sdk\gtest\include\gtest/gtest.h(1316): error C2784: 'bool testing::internal::operator ==(T *,const testing::internal::linked_ptr<T> &)' : could not deduce template argument for 'T *' from 'const Foo'
1>          D:\3rdpartycache\CPP\gmock\1.6.0-2\sdk\gtest\include\gtest/internal/gtest-linked_ptr.h(213) : see declaration of 'testing::internal::operator =='
1>          D:\3rdpartycache\CPP\gmock\1.6.0-2\sdk\gtest\include\gtest/gtest.h(1353) : see reference to function template instantiation 'testing::AssertionResult testing::internal::CmpHelperEQ<T1,T2>(const char *,const char *,const T1 &,const T2 &)' being compiled
1>          with
1>          [
1>              T1=Foo,
1>              T2=Foo
1>          ]
1>          OperationsOnFooTest.cpp(146) : see reference to function template instantiation 'testing::AssertionResult testing::internal::EqHelper<lhs_is_null_literal>::Compare<Foo,T>(const char *,const char *,const T1 &,const T2 &)' being compiled
1>          with
1>          [
1>              lhs_is_null_literal=false,
1>              T=Foo,
1>              T1=Foo,
1>              T2=Foo
1>          ]
1>          OperationsOnFooTest.cpp(146) : see reference to function template instantiation 'testing::AssertionResult testing::internal::EqHelper<lhs_is_null_literal>::Compare<Foo,T>(const char *,const char *,const T1 &,const T2 &)' being compiled
1>          with
1>          [
1>              lhs_is_null_literal=false,
1>              T=Foo,
1>              T1=Foo,
1>              T2=Foo
1>          ]

1 个答案:

答案 0 :(得分:3)

尝试验证您的operator ==和Foo类是否在同一名称空间中。 我遇到了类似的错误,解决了这个问题。

你对ASSERT_EQ编译错误信息是正确的......虽然不是很有用......