Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Assert::AreEqual in the VS UnitTesting framework work with std::string?

I am trying to do a unit test on some c++ code but am running into some trouble.

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'm getting the following compiler error...

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

It seems like it should be a match with the following overload:

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

Isn't the above overload a template overload that should support any object, as long as arguments 1 and 2 are of the same type? Or am I missing something?

Is there some other way that I can accomplish this Assert?

like image 789
sgryzko Avatar asked Sep 27 '12 19:09

sgryzko


2 Answers

You're attempting to use the managed unit testing framework with native types – this simply isn't going to work without marshaling the objects into managed types first.

VS2012 now comes with a native C++ unit testing framework; using this framework instead, your code could work by changing "Unexpected Object Name" to a wide string (prefix with L) and calling the following overload:

template<typename T> 
static void AreEqual(
    const T& expected, 
    const T& actual, 
    const wchar_t* message = NULL, 
    const __LineInfo* pLineInfo = NULL)
like image 88
ildjarn Avatar answered Sep 19 '22 08:09

ildjarn


If we are trying to stay in un-managed C++, and we don't care what the error message looks like, this is probably a better option than the accepted answer:

Assert::IsTrue(s1==s2)

By better, I mean it is at least easy to read.

like image 29
M. K. Hunter Avatar answered Sep 19 '22 08:09

M. K. Hunter