Reputation: 2407
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?
Upvotes: 9
Views: 10735
Reputation: 993
I believe solution is to us L prefix before string
Assert::AreEqual<bool>(true, dict->IsBeginWith("", ""), L"Empty");
You can also try case it like this, which give you wrong results, but lead to right direction of understanding of issue
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
Upvotes: 1
Reputation: 1828
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.
Upvotes: 3
Reputation: 2407
I hacked up a bit of a workaround so that integers are compared instead of strings:
Assert::AreEqual(0, s1.compare(s2), "Unexpected Object Name");
In the future, we will likely switch to native C++ unit testing, but in the meantime, this does the trick. Obviously the messaging for this isn't very helpful
Assert.AreEqual failed. Expected:<0>. Actual:<1>. Unexpected Trajectory Name
But it's better than nothing.
Upvotes: 1
Reputation: 62975
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)
Upvotes: 4