Javascript - Mocha Chai单元测试无法运行
P粉964682904
P粉964682904 2024-04-02 14:52:19
0
1
361

我正在与前端 Web 开发课程训练营合作开展一个项目,不幸的是,我们的教授无法回答我的这个问题。

我有一个简单的战争纸牌游戏,需要两名玩家,然后玩 26 个回合,看看谁赢了。在这个游戏中我有 4 个类,如下

  • 卡片
  • 甲板
  • 玩家
  • 战争

这些都被设置为模块,以便可以导出然后导入到需要它们运行的​​所有相应 JS 文件中。

然后我有一组单元测试文件,然后它们将尝试使用每个相应的文件,然后生成 tests.html 的测试

这将访问每个相应的单元测试 js 文件,然后通过描述方法运行。但是,它们实际上不会为单元测试运行相应的“it”函数(这是通过描述和 it 函数中的 console.log() 验证的。当我尝试在 Web 浏览器上转到控制台时,它也会这样做不抛出任何错误进行调试。

第一个链接是我在第 6 周提交的主分支,您将看到当我不导入时测试会起作用,然后在描述中具有相应的类。 https://github.com/jeffhennen/Projects/tree/master/Week6/Week6Final

我希望它使用文件中的实时代码而不是单元测试,同时也将每个相应的文件分开,就像我在本节中那样。 https://github.com/jeffhennen/Projects/tree/Test/Week6/Week6Final

这是我的测试.html

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="node_modules/mocha/mocha.css">
</head>
<body>
    <div id = "mocha"><p><a href=".">Index</a></p></div>
    <div id = "messages"></div>
    <div id = "fixtures"></div>
    <script src="node_modules/mocha/mocha.js"></script>
    <script src="node_modules/chai/chai.js"></script>
    <script type="module" src="Scripts/Card.js"></script>
    <script type="module" src="Scripts/Deck.js"></script>
    <script type="module" src="Scripts/Player.js"></script>
    <script type="module" src="Scripts/War.js"></script>
    <script>mocha.setup('bdd')</script>
    <script type="module" src="UnitTests/CardTest.js"></script> 
    <script type="module" src="UnitTests/DeckTest.js"></script>
    <script type="module" src="UnitTests/PlayerTest.js"></script>
    <script>mocha.run();</script>

</body>
</html>

下面是我的 CardTest.js 文件的示例

var expect = chai.expect;
import Card from '../Scripts/Card.js';

describe('Card Functions', () => {

    describe('Constructor', () => {

        console.log("Inside card describe constructor");
        let card = new Card("Club", "King", 13);
        console.log(card);
        it('Should create the card with the value of the card\'s suit equal to param 0', () => {

            console.log("test1");
            expect(card._suit).to.equal("Club");
        });

        it('Should create the card with the value of the card\'s string value equal to the param 1', () => {

            console.log("test2");
            expect(card._number).to.equal("King");
        });

        it('Should assign the numeric value of the card\'s string value to the card object', () => {

            console.log("test3");
            expect(card._value).to.equal(13);
        });
    });
});

这是我的 Card.js 文件

class Card{

    constructor(suit, number, value){

        this._suit = suit;
        this._value = value;
        this._number = number;
    }

    get suit(){

        return this._suit;
    }

    get value(){

        return this._value;
    }

    get number(){

        return this._number;
    }
    
}
export default Card;

P粉964682904
P粉964682904

全部回复(1)
P粉786800174

我发现要让它运行,我需要做的就是将tests.html更新为以下内容,我只需将标签更新为

sssccc






    


    
    
sssccc sssccc sssccc sssccc sssccc sssccc sssccc sssccc sssccc sssccc sssccc
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!