VS2012:单元测试出错:Assert :: AreEqual(object,object)不起作用 [英] VS2012 : Error with unit test : Assert::AreEqual( object, object ) didn't work

查看:132
本文介绍了VS2012:单元测试出错:Assert :: AreEqual(object,object)不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在VS 2012上使用Visual Studio本机单元测试时遇到一个奇怪的问题。
我有一个这样的Coordinates类:

I come to you for a strange problem when I use the Visual Studio Native Unit Test on VS 2012. I've a Coordinates class like that:

#ifndef COORDINATES_HPP
#define COORDINATES_HPP

#include <iostream>

namespace Core {
class Coordinates {
public:
    Coordinates();
    Coordinates( int x, int y );
    Coordinates( const Coordinates &copy );
    ~Coordinates();

    void operator=( Coordinates coordinates );
    void operator+=( Coordinates coordinates );
    void operator-=( Coordinates coordinates );
    Coordinates operator+( Coordinates coordinates );
    Coordinates operator-( Coordinates coordinates );
    bool operator==( Coordinates coordinates );
    bool operator!=( Coordinates coordinates );

    int getX() const { return m_x; }
    int getY() const { return m_y; }
    void setX( const int &val ) { m_x = val; }
    void setY( const int &val ) { m_y = val; }

protected:
    int m_x, m_y;
};
}

所以当我使用时出现问题:
Assert :: AreEqual (Coordinates(0,0),Coordinates(0,0));

So the problem appear when I use : Assert::AreEqual( Coordinates(0,0), Coordinates(0,0) );

发送的错误为:
错误1错误C2678:二进制'==' :没有找到采用类型为'const Core :: Coordinates'的左侧操作数的运算符(或没有可接受的转换)c:\程序文件(x86)\ Microsoft Visual Studio 11.0\vc\unittest\ \include\cppunittestassert.h 129 1 UnitTest1

The error sended is : Error 1 error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const Core::Coordinates' (or there is no acceptable conversion) c:\program files (x86)\microsoft visual studio 11.0\vc\unittest\include\cppunittestassert.h 129 1 UnitTest1

您有解决的办法吗?

PS :对不起,我的英语不是我的母语。

PS: Sorry for my english, is not my native language.

推荐答案

创建分配运算符(即

The error received after creating your assignment operator, ie


错误1错误C2338:测试编写者必须为类class std :: basic_string,class std :: allocator定义ToString的特化> __cdecl Microsoft :: VisualStudio :: CppUnitTestFramework :: ToString(const class Core :: Coordinates&)。

Error 1 error C2338: Test writer must define specialization of ToString for your class class std::basic_string,class std::allocator > __cdecl Microsoft::VisualStudio::CppUnitTestFramework::ToString(const class Core::Coordinates &).

与需要为单元测试提供一种打印期望和接收的值的方法有关。通过在 Microsoft :: VisualStudio :: CppUnitTestFramework 命名空间中创建ToString函数的模板特化来实现。例如:

is related to needing to provide a way for the unit tests to print out the values it expected and received. You do this by creating a template specialization of the ToString function in the Microsoft::VisualStudio::CppUnitTestFramework namespace. For example:

namespace Microsoft{
    namespace VisualStudio {
        namespace CppUnitTestFramework {

            template<>
            static std::wstring ToString<Coordinates>(const Coordinates  & coord) {
                return L"Some string representing coordinate.";
            }

        }
    }
}

之后,应该运行单元测试。

After that, the unit tests should run.

这篇关于VS2012:单元测试出错:Assert :: AreEqual(object,object)不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆