首页  >  问答  >  正文

Javascript - Mocha Chai单元测试无法运行

我正在与前端 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粉964682904P粉964682904181 天前370

全部回复(1)我来回复

  • P粉786800174

    P粉7868001742024-04-03 11:37:23

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

    sssccc
    
    
    
    
    
    
        
    
    
        
        
    sssccc sssccc sssccc sssccc sssccc sssccc sssccc sssccc sssccc sssccc sssccc

    回复
    0
  • 取消回复