Marty Wallace
Marty Wallace

Reputation: 35734

Structuring Mocha test with objects

Trying to learn some js and mocha and am sure im doing something fundamentally wrong. For example:

Product model:

var Product = function (sku) {
    this.sku = sku;
};

Product.prototype.getSku = function() {
    return this.sku;
}

module.exports = {Product: Product};

and my tests:

var productLib = require('../../model/Product.js');
var assert = require('assert')

describe("Product", function() {
    describe('#getSku()', function() {

        var product = new productLib.Product('test_sku');
        it('should have an getSku method', function() {
            assert.equal(typeof product.getSku, 'function');
        });
        it('should return the loaded sku', function() {
            assert.equal(product.getSku(), 'test_sku');
        });
    });
});

Is this correct in terms of how the product model is structured and exported? It seems wrong but i had to do it this way so that in the test I could do:

var product = new productLib.Product('test_sku');

and have the assertion that the sku was set correctly.

Upvotes: 0

Views: 729

Answers (1)

sctskw
sctskw

Reputation: 1598

There's absolutely nothing wrong with how you're setting up your module if that's what you're asking. The only thing that I see that you could possibly change might be how you export your Product model:

From this:

module.exports = {Product: Product};

To this:

module.exports = Product;

Which you would just use like so:

var ProductModel = require('../../model/Product');
var model = new ProductModel(...);

Upvotes: 1

Related Questions