Is it possible to check if a generic nullptr pointer returns in a native unit test?

Using Visual Studio 2012 native unit test, code cannot be compiled using a shared pointer.

My code is:

#include "stdafx.h" #include "CppUnitTest.h" #include "ParameterTest.h" using namespace Microsoft::VisualStudio::CppUnitTestFramework; using namespace MyParameter; namespace MyParameterTests { TEST_CLASS(MyParameterTests) { public: TEST_METHOD(CheckParametersWithPathReturnsNotNull) { //Arrange Parameter parameter = Parameter(); //Act std::shared_ptr<std::string> actual = parameter.CheckParameters("C:\\Parameter.db"); //Assert Assert::IsNotNull(actual, L"Should not return null", LINE_INFO()); } }; } 

Compiler emulator:

2> ------ Rebuild It all started: Project: MyParameterTests, Configuration: Debug Win32 ------ 2> stdafx.cpp 2> Parameter TestTests.cpp 2> c: \ users \\ documents \ visual studio 2012 \ projects \ myparametertests \ parametertests.cpp (26): error C2784: 'void Microsoft :: VisualStudio :: CppUnitTestFramework :: Assert :: IsNotNull (const T *, const wchar_t *, const Microsoft :: VisualStudio :: CppUnitTestFramework :: __ LineInfo *) ': could not derive the template argument for' const T * 'from' std :: shared_ptr <_Ty> '2> with 2> [2> _Ty = std :: string2>] 2> c: \ program files (x86 ) \ microsoft visual studio 11.0 \ vc \ unittest \ include \ cppunittestassert.h (259): see the announcement "Microsoft :: VisualStudio :: CppUnitTestFramework :: Assert :: IsNotNull"

+4
source share
1 answer

You must replace

 Assert::IsNotNull(actual, L"Should not return null", LINE_INFO()); 

with

 Assert::IsNotNull(actual.get(), L"Should not return null", LINE_INFO()); 

In fact, the statement wants the pointer to check if it is null, but the smart pointer is not a pointer strictly speaking (it is an object that controls the pointer).

+8
source

Source: https://habr.com/ru/post/1484629/


All Articles