0
我有一個類,我們假設它被命名爲Foo
其中我沒有定義一個相等運算符,我不想定義一個(出於我自己的原因)。谷歌測試ASSERT_EQ不編譯
我想測試其在富操作某些功能,我寫了下面的代碼:
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> ]
顯示'Foo'的聲明。 – trojanfoe
它沒有什麼意思。帶int和雙字段的POD。沒有矢量/字符串/指針/花哨的東西。 – Alex
問題顯然在於你初始化'expected'和'actual'的「= ...」。如果'Foo'被賦予了一個你認爲不重要的POD實現,那麼你的全局運算符==(const Foo&left,const Foo&right)'就會被賦予objvious implmentation,而那些「= ...」s被任何一對簡單的初始化程序替換,然後代碼編譯。給出一個完整的代碼示例。否則,沒有足夠的信息來回答問題,因此應該關閉。 –