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

发布于 2025-02-09 23:24:28 字数 4136 浏览 2 评论 0原文

单位测试问题

我无法真正看到该代码出了什么问题,但是某些内容不正确 相关文件:

metadata.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

#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

测试文件。

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

因此,我遇到的错误是

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

一些警告

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  

错误2

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

我制作了一个项目,添加了一些基本文件

并想进行测试驱动的开发。
因此,我在当前的项目中添加了一个本地单元测试项目。 此处描述: MS描述要添加单元测试

我以前用很多语言编码,但是对Java/c#的经验最多(您知道,这些与CPP有很大不同),

我感谢每个评论/输入事情。

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

#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

#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

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

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

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)

I appreciate every comment/input for that matter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

再见回来 2025-02-16 23:24:28

我注意到您使用了#include&lt; metadata.h&gt;

使用添加现有项目添加元数据.h和.cpp。或者,您需要为元数据构建静态库。

I noticed that you used #include <MetaData.h>.

Use Add Existing item to add MetaData .h and .cpp. Or, you need to build static library for MetaData.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文