简体   繁体   中英

Basic mocha TDD interface

Problem

I have the following file in javascript:

function myLocalHouse() {

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


};

I want to test the buyHouse method.

What I have tried

I have tried the most easy thing in the world, but as hard as I try It doesn't work

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');
    });
});

Unfortunately this doesn't work, when I execute mocha it says method buyHouse is undefined.

I execute the test with:

mocha -u tdd

Thanks

You need to export your function in myLocalHouse.js. Try something like this:

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

or

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

exports.myLocalHouse = myLocalHouse;

then in your test

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');
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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