Why does Assert :: AreEqual in VS UnitTesting structure work with std :: string?

I am trying to do a unit test for some C ++ code, but I am having problems.

I have something similar to the following lines of code ...

std::string s1 = obj->getName();
std::string s2 = "ExpectedName";
Assert::AreEqual(s1, s2, "Unexpected Object Name");

And I get the following compiler error ...

error C2665: 'Microsoft::VisualStudio::TestTools::UnitTesting::Assert::AreEqual' :
none of the 15 overloads could convert all the argument types

This seems to coincide with the following overload :

AreEqual<(Of <(T>)>)(T, T, String) 

Not an overload of the above template overload, which should support any object if arguments 1 and 2 are of the same type? Or am I missing something?

Is there any other way I can fulfill this statement?

+5
source share
4 answers

I hacked a bit of a workaround so that integers are compared instead of strings:

Assert::AreEqual(0, s1.compare(s2), "Unexpected Object Name");

, , ++, . ,

Assert.AreEqual failed. Expected:<0>. Actual:<1>. Unexpected Trajectory Name

, .

+1

- .

VS2012 ++; , "Unexpected Object Name" ( L) :

template<typename T> 
static void AreEqual(
    const T& expected, 
    const T& actual, 
    const wchar_t* message = NULL, 
    const __LineInfo* pLineInfo = NULL)
+4

++, , , , , , :

Assert::IsTrue(s1==s2)

, , , .

+3

I believe that the solution for us is the prefix L before the line

      Assert::AreEqual<bool>(true, dict->IsBeginWith("", ""), L"Empty");

You can also try a case like this, which gives incorrect results, but leads to the right direction of understanding the problem.

   Assert::AreEqual<bool>(true, dict->IsBeginWith("", ""), (wchar_t*)"Empty"); //Empty
   Assert::AreEqual(true, dict->IsBeginWith("A", "A"), (wchar_t*)"Empty2");
   Assert::AreEqual(true, dict->IsBeginWith("A", "a"), (wchar_t*)""); //CAPITAL LETTER Check
0
source

All Articles