简体   繁体   English

C++ 中的单元测试问题 (Visual Studio 2022)

[英]Problems with Unit Testing in C++ (Visual studio 2022)

Issues with unit testing单元测试的问题

I cant really see what is going wrong with that code, but something doesn't include right relevant files:我真的看不出该代码出了什么问题,但有些东西不包括正确的相关文件:

MetaData.h元数据.h

#pragma once
#include <string>
enum ChessColor
{
    White,
    Black
};
enum PieceType
{
   King,
   Queen,
   Rook,
   Bishop,
   Knight,
   Pawn,
   None
};
const int BOARD_SIZE = 8;

std::string getLongNameOfChessType(PieceType* type);
std::string getShortNameOfChessType(PieceType* type);

std::string getLongNameOfChessColor(ChessColor* color);
std::string getShortNameOfChessColor(ChessColor* color);

MetaData.cpp元数据.cpp

#include "MetaData.h"

std::string getLongNameOfChessType(PieceType* type)
{
    switch (*type)
    {
    case PieceType::King:
        return "King";
    case PieceType::Queen:
        return "Queen";
    case PieceType::Rook:
        return "Rook";
    case PieceType::Bishop:
        return "Bishop";
    case PieceType::Knight:
        return "Knight";
    case PieceType::Pawn:
        return "Pawn";
    default:
        return "NoType";
    }
}

std::string getShortNameOfChessType(PieceType* type)
{
    switch (*type)
    {
    case PieceType::King:
        return "K";
    case PieceType::Queen:
        return "Q";
    case PieceType::Rook:
        return "R";
    case PieceType::Bishop:
        return "B";
    case PieceType::Knight:
        return "N";
    case PieceType::Pawn:
        return "P";
    default:
        return "NoType";
    }
}
std::string getLongNameOfChessColor(ChessColor* color)
{
    switch (*color)
    {
    case ChessColor::White:
        return "White";
    case ChessColor::Black:
        return "Black";
    default:
        return "NoColor";
    }
}
std::string getShortNameOfChessColor(ChessColor* color)
{
    switch (*color)
    {
    case ChessColor::White:
        return "W";
    case ChessColor::Black:
        return "B";
    default:
        return "NoColor";
    }
}

ChessTest.cpp国际象棋测试.cpp

The test file.测试文件。

#include "pch.h"
#include "CppUnitTest.h"
#include <MetaData.h>
using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace ChessTest
{
    TEST_CLASS(ChessTest)
    {
    public:
        
        TEST_METHOD(TestMethod1)
        {
            ChessColor c = ChessColor::Black;
            std::string actual = "B";
            std::string trying = getShortNameOfChessColor(&c);
            Assert::AreEqual(actual,trying);
        }
    };
}

So the errors i get are these所以我得到的错误是这些

Error   LNK2019 unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl getShortNameOfChessColor(enum ChessColor *)" (?getShortNameOfChessColor@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PEAW4ChessColor@@@Z) referenced in function "public: void __cdecl ChessTest::ChessTest::TestMethod1(void)" (?TestMethod1@ChessTest@1@QEAAXXZ)    ChessTest   C:\Users\censoredUsername\Desktop\prog\Chess\ChessProject\ChessTesting\ChessTest\ChessTest.obj  1

A few warnings一些警告

Warning C26812  The enum type 'PieceType' is unscoped. Prefer 'enum class' over 'enum' (Enum.3).    ChessProject    C:\Users\censoredUsername\Desktop\prog\Chess\ChessProject\ChessProject\MetaData.cpp 3
Warning C26812  The enum type 'ChessColor' is unscoped. Prefer 'enum class' over 'enum' (Enum.3).   ChessProject    C:\Users\censoredUsername\Desktop\prog\Chess\ChessProject\ChessProject\MetaData.cpp 44  
Warning C26812  The enum type 'ChessColor' is unscoped. Prefer 'enum class' over 'enum' (Enum.3).   ChessTest   C:\Users\censoredUsername\Desktop\prog\Chess\ChessProject\ChessTesting\ChessTest\ChessTest.cpp  14  

Error 2错误 2

Error   LNK1120 1 unresolved externals  ChessTest   C:\Users\censoredUsername\Desktop\prog\Chess\ChessProject\x64\Debug\ChessTest.dll   1   

Some Extra info一些额外的信息

I made a project, added some basic files and wanted to do a test driven development.我做了一个项目,添加了一些基本文件,并想做一个测试驱动的开发。
Thus i added a Native Unit Test Project to my current Project.因此,我在当前项目中添加了一个本机单元测试项目。 Described here: MS Description to add unit tests此处描述: 添加单元测试的 MS 描述

I coded in a lot of languages before, but have the most experience with java/c# (and as you know, these are very different to cpp)我以前用过很多语言编写过代码,但对 java/c# 最有经验(如你所知,这些与 cpp 非常不同)

I appreciate every comment/input for that matter.我感谢每条评论/意见。

I noticed that you used #include <MetaData.h> .我注意到您使用了#include <MetaData.h>

Use Add Existing item to add MetaData .h and .cpp.使用Add Existing item添加 MetaData .h 和 .cpp。 Or, you need to build static library for MetaData.或者,您需要为 MetaData 构建静态库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM