简体   繁体   English

无法识别Mocha中的构造函数

[英]Constructor Function in Mocha not recognised

I'm completely out of ideas as to what is going on here so hopefully someone can help. 我对这里发生的事情完全不了解,希望有人可以提供帮助。

Here is my script that I'm testing; 这是我正在测试的脚本;

function MyFunc() {

    this.test = 'hello world';

};

module.exports = MyFunc;

Here is my test; 这是我的测试;

var expect = require('chai').expect;
var MyFunc = require('../myfunc.js');

describe('MyFunc', function() {

    it('test should equal to Hello World', function() {
        var subject = new MyFunc;
        expect(subject.test).to.eql('hello world');
    });

})

When I run the test it fails and it gives me this message; 当我运行测试时,它失败了,并且给了我这个信息。

TypeError: MyFunc is not a function

I've simplified the code just to get my problem across, but I've tried several variations to no avail. 我简化了代码只是为了解决我的问题,但是我尝试了几种变体但无济于事。 I've spent quite a bit of time looking for similar issues with no joy (usually that means it's something stupid I'm doing. Lets hope). 我花了很多时间没有高兴地寻找类似的问题(通常这意味着我在做些愚蠢的事情。希望如此)。

I test my Angular code in a very similar way with no issues at all. 我以非常相似的方式测试我的Angular代码,完全没有问题。 I'm stumped! 我很沮丧!

Thanks for taking the time to read, 感谢您抽出宝贵的时间阅读,

Anthony 安东尼

Change var subject = new MyFunc ===> var subject = new MyFunc() 更改var subject = new MyFunc ===> var subject = new MyFunc()

 describe('MyFunc', function() { it('test should equal to Hello World', function() { var subject = new MyFunc(); expect(subject.test).to.eql('hello world'); }); }) 

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

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