是否可以检查共享指针是否在本机单元测试中返回nullptr?

Mat*_*tin 4 c++ unit-testing visual-studio-2012

使用Visual Studio 2012本机单元测试时,使用共享指针时无法编译代码.

我的代码:

#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());

        }

    };
}
Run Code Online (Sandbox Code Playgroud)

编译器错误:

2> ------ Rebuild All started:Project:MyParameterTests,Configuration:Debug Win32 ------ 2> stdafx.cpp 2> ParameterTestTests.cpp 2> c:\ users \\ documents\visual studio 2012\projects\myparametertests\parametertests.cpp(26):错误C2784:'void Microsoft :: VisualStudio :: CppUnitTestFramework :: Assert :: IsNotNull(const T*,const wchar_t*,const Microsoft :: VisualStudio :: CppUnitTestFramework :: __ LineInfo*)':无法从'std :: shared_ptr <_Ty>'2>中推断'const T*'的模板参数2> [2> _Ty = std :: string 2>] 2> c:\ program files(x86) )\ microsoft visual studio 11.0\vc\unittest\include\cppunittestassert.h(259):查看'Microsoft :: VisualStudio :: CppUnitTestFramework :: Assert :: IsNotNull'的声明

JBL*_*JBL 8

你应该更换

Assert::IsNotNull(actual, L"Should not return null", LINE_INFO()); 
Run Code Online (Sandbox Code Playgroud)

Assert::IsNotNull(actual.get(), L"Should not return null", LINE_INFO());
Run Code Online (Sandbox Code Playgroud)

实际上,断言需要一个指针来检查它是否为空,但智能指针严格来说不是一个指针(它是一个管理指针的对象).