问题

我在javascript中有以下文件:

function myLocalHouse() {

this.buyHouse(money, date) {
   //code I want to test with mocha
};


};


我想测试buyHouse方法。

我尝试过的

我尝试了世界上最简单的事情,但是尽我所能,这是行不通的

var myLocalHouseInstance = require('/myLocalHouse.js');

suite('houses suite', function() {
    test('test that buy House works correctly', function() {
       var something = myLocalHouseInstance.buyHouse(100, '17/08/2013');
    });
});


不幸的是,这不起作用,当我执行mocha时,它说方法buyHouse未定义。

我执行以下测试:

mocha -u tdd

谢谢

最佳答案

您需要在myLocalHouse.js中导出函数。尝试这样的事情:

exports.myLocalHouse = function() {
    this.buyHouse = function(money, date) {
    }
}


要么

function myLocalHouse() {
    this.buyHouse = function(money, date) {
    }
}

exports.myLocalHouse = myLocalHouse;


然后在你的测试

var myLocalHouse = require('./myLocalHouse.js').myLocalHouse;

suite('houses suite', function() {
    test('test that buy House works correctly', function() {
        var myLocalHouseInstance = new myLocalHouse();
        var something = myLocalHouseInstance.buyHouse(100, '17/08/2013');
    });
});

09-11 15:31