首頁  >  問答  >  主體

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粉964682904180 天前364

全部回覆(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
  • 取消回覆