简体   繁体   English

带有Mocha,Chai的函数时如何测试Javascript对象属性

[英]How to test a Javascript object property when function with mocha, chai

Trying to test a Javascript object 尝试测试Javascript对象

Game.js Game.js

var GameManager= {
    gameType: 'room',
    roomDimension: [10,10],
    playerDirection: ["W"]
    possibleDirections: ["N","E","S","W"],
    init: function(){
        if(this.gameType == 'room'){
            regexpNumber = /^-?[0-9]$|-?([1][0-9])$/;
        }
    return true;
    },
    turnRight : function(){
    var movePosition = this.possibleDirections.indexOf(this.playerDirection);
    if(movePosition == this.possibleDirections.length -1 ){
        return this.possibleDirections[0];
    }
    return this.possibleDirections[movePosition + 1];
  },
    commandString: function(string){
    var command = /^[PQR]*$/;

    if(command.test(string){
        return true;
    }
    this.errorMessageNumber = 0;
    return false;
}
}

Here is my test script test.js 这是我的测试脚本test.js

var expect = require("chai").expect;
var GameManager = require("../GameManager.js");

describe("Test game type", function() {
   beforeEach(function () {
      GameManager.gameType = 'room';
      GameManager.roomDimension= [10, 10];
      GameManager.possibleDirections: ["N","E","S","W"];
  });

  it('should commandString be as parameters', function() {
    expect(GameManager.commandString("AABB")).to.not.be.false;
  });

  it('should init toBeTruthy', function() {
    expect(GameManager.init()).ok;
  });
});

Issue: In both cases the test fail with TypeError error is shown as below for one of the test: 问题:在两种情况下,针对其中一项测试的测试均失败,并显示TypeError错误:

  1) Test game type should init toBeTruthy:
  TypeError: GameManager.init is not a function at Context.<anonymous>

Since init is not considered as function here, how can this be tested? 由于此处未将init视为功能,如何进行测试?

Why are you trying to test a simple object? 为什么要尝试测试一个简单的对象? I think, although it can be done, the UT are more intended to be behavioral tests. 我认为,尽管可以做到,但UT更适合作为行为测试。

That said, I think you're using the assert wrong: 也就是说,我认为您使用的是断言错误:

it('should init toBeTruthy', function() {
   expect(GameManager.init()).to.be.ok;
});

Or to be more precise with the test: 或更准确地说,测试:

it('should init toBeTruthy', function() {
   expect(GameManager.init()).to.be.true;
});

Also, you may refer to this post, it might be helpful 另外,您可以参考这篇文章,可能会有所帮助

Why is my mocha/chai Error throwing test failing? 为什么我的Mocha / chai错误抛出测试失败?

You said in a comment: 您在评论中说:

I am not exporting GameManager since it is a simple Javascript object. 我没有导出GameManager因为它是一个简单的Javascript对象。 Not using as Typescript export class. 不用作Typescript导出类。

You still need to export it. 您仍然需要导出它。 Whether it is a simple JavaScript object or not is utterly irrelevant. 无论它是不是简单的JavaScript对象,都完全无关紧要。 You have to write your code in a way that works with Node.js' module system. 您必须以与Node.js的模块系统兼容的方式编写代码。

Using the code you show in the question, and fixing the syntax errors in it, I can get your second test (the one for init ) to work if I add this to your GameManager.js file: 使用问题中显示的代码,并修复其中的语法错误,如果将其添加到GameManager.js文件中,则可以使第二个测试(用于init测试)正常工作:

module.exports = GameManager;

With the code you show in the question, the exported value from GameManager.js is {} . 使用问题中显示的代码, GameManager.js的导出值为{} So when you do var GameManager = require("../GameManager.js"); 因此,当您执行var GameManager = require("../GameManager.js"); the GameManager variable gets assigned the value {} . GameManager变量被分配了值{} Consequently GameManager.init has the value undefined , and thus it is not a function. 因此, GameManager.init的值为undefined ,因此它不是一个函数。 Adding the line I show above, will make it so that the GameManager variable in your test file gets the value of GameManager from your GameManager.js file, and the test will pass. 添加上面显示的行,即可使测试文件中的GameManager变量从GameManager.js文件中获取GameManager的值,然后测试就会通过。

(You did not provide code for GameManager.commandString so the first test will still fail. Just add the code for it to get it to work.) (您没有提供GameManager.commandString代码,因此第一个测试仍然会失败。只需为其添加代码即可使其工作。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM