Jasmine测试没有创建类

时间:2018-04-29 16:07:38

标签: javascript class testing jasmine

我试图将Jasmine测试应用到我的JS程序(只是一个游戏),并且由于某种原因它没有创建新实例。也许有些人可以帮我找到错误。

'use strict';


describe('logic(bingo)',function() {
    var bin
    beforeEach(function() {
        bin = new Bingo('Alex')
    })

    it('should throw error if name is not a string',function() {
        bin=new Bingo()
        expect(function() {
            return bin.name
        }).toThrowError('Input a string name')
    })

    it('should board.length===3 and board[0].length===5',function() {
        expect(bin.board.length).toBe(3)
        expect(bin.board[0].length).toBe(5)
    })
    }

bin不会存储新的Bingo"。它一直没有定义。此类Bingo在不同的文件中声明,链接是正确的。

提前致谢。

1 个答案:

答案 0 :(得分:0)

如果未提供名称或不是字符串,您如何抛出错误?

我认为它应该在构造函数中,例如:

...
if(!name || typeof name !== 'string') {
  throw new Error('Input a string name');
}
...

并且还检查它实例化而不是getter:

    it('should throw error if name is not a string',function() {
      expect(function() {
        new Bingo(1)
      }).toThrowError('Input a string name')
    })

这是一个有效的插件http://plnkr.co/edit/Z4wT9s

相关问题