您将如何对这种功能进行单元测试?

时间:2019-03-19 16:19:50

标签: php unit-testing

您将如何对这种功能进行单元测试?

class Game {
    constructor(player, client, channelName='bot-testing', playersOnline){
         this.client = client;
         this.channelName = channelName;
         this.currentPlayer = player;   
         this.playersOnline = [];
         this.hitpoints = 120;
         this.damage = '';
         this.chance = 3;
         this.inBattle = false;
         this.online = playersOnline;

        this.monster = [{
            hp: Math.floor(Math.random() * 200),
            temphp: 0,
            damage: 10
        }];
    };

    /* main menu information, players online */
    startGame(){
            for(var x = 0; x < this.currentPlayer.length; x++){
                this.playersOnline.push(this.currentPlayer[x]);
                if(this.playersOnline[x] === this.currentPlayer[x]){
                    return [`Players Online: ${this.online}\n`];
            }
         }
    }

    /* Battle system */
    initBattle(currPlayer){
        this.inBattle = true;
        let npcHP = this.monster[0].hp;
        let numberOfAttacks = 0;
        let totalDamage=0, totalBonusDamage=0;

        while( this.monster[0].hp > 0 ){
            let playerDamage = Math.floor(Math.random() * (npcHP / 4));  
            if(this.bonusAttack() === 2){
                console.log(`Bonus Attack: ${this.bonusAttack()}`);
                console.log(`Regular damage without bonus attack: ${playerDamage}`);
                playerDamage = playerDamage + 2; 
            }
            this.monster[0].hp -= playerDamage;
            this.hitpoints -= this.monster[0].damage;

            console.log('Monster: ' + this.monster[0].hp);
            console.log('Player: ' + this.hitpoints);
            console.log(`${currPlayer} has attacked for ${playerDamage}`);
            console.log(`NPC health: ${this.monster[0].hp}`);   

            if(this.hitpoints <= 0){
                return [`You lost the battle.`];
            }

            this.inBattle = false;
            numberOfAttacks++; 
            totalDamage += playerDamage;
            totalBonusDamage = playerDamage + this.bonusAttack();    
        }
        if(this.monster[0].hp <= 0 && this.inBattle !== true){
            let maxDamage = totalDamage + totalBonusDamage; 
            return [`${currPlayer} has attacked ${numberOfAttacks} times dealing ${totalDamage} + (${totalBonusDamage}) bonus damage for a total of ${maxDamage} damage. The monster is dead.\n
            Your Health: ${this.hitpoints}`];
        } 
        else{
            this.newGame();
            return [`You rejuvenated your hitpoints and are ready for battle. \nType !fight again to start a new battle!`];
        }
    }

    /* bonus attack damage [ 1 in 3 chance ] */
    bonusAttack(bonusDamage){
        let chance = Math.floor(Math.random() * 3);
        return chance === 2 ? bonusDamage = 2 : false;
    }

    /* displays players currently online */
    getOnline(){
        console.log(this.currentPlayer);
        return this.currentPlayer;

    }

    /* refresh stats */
    newGame(){
        this.monster[0].hp = Math.floor(Math.random() * 50);
        this.hitpoints = 150;
    }
}

module.exports = Game;

我有模拟对象和public function import(int $id, array $fields, string $data) { try { $file = $this->writeTemporaryFile(string $data); if (!$this->getStorage()->ping()) { throw new \Exception('Unable ping storage'); } $result = $this->getStorage()->importFromFile($fields, $file); if (!$result) { throw new \Exception('Unable to write to storage'); } $this->getAnotherStorage()->ok($id); return true; } catch (\Throwable $e) { $this->log($e); $this->failsafe($data); return false; } } $this->getStorage()返回的对象的存根。拥有$this->getAnotherStorage()$this->getStorage()->ping()等的存根。

所以,问题是:

a)您会注意哪些时刻?您将测试所有方法的可能抛出异常,还是仅测试$this->getAnotherStorage()->ok()方法本身的结果?

b)您将如何组织单元测试的代码?大胖的单一方法...

import

还是调用受保护方法的单个方法,每种情况一个?

public function testImport(): void {
    //here goes a big fat testing method
}

0 个答案:

没有答案