Rodrigo
Rodrigo

Reputation: 3278

unit testing with mocha not finding function

So I'm trying to unit test a function I wrote here is the function:

function compareServices(service1, service2) {
  if (service1.name != service2.name) {
    return false
  }
  if (service1.port != service2.port) {
    return false
  }
  if (service1.target != service2.target) {
    return false
  }
  if (service1.address != service2.address) {
    return false
  }

  return true
}

it is located inside my main.js file. Now When I try to run the test I get an error saying:

 Main #compareServices() should compare if two services match:
 TypeError: main.compareServices is not a function
  at Context.<anonymous> (test/stelaSpec.js:21:25

And here is my test file:

var expect = require("chai").expect;
var main = require("../src/main.js");

describe("Main", function(){
  describe('#compareServices()', function() {
    it('should compare if two services match', function() {
      var svc1 = {
        "name": "test.fg.service",
        "port": 8001,
        "target": "mac-book",
        "address": "192.2.2.2"
      }

      var svc2 = {
        "name": "test.fg.service",
        "port": 8001,
        "target": "mac-book",
        "address": "192.2.2.2"
      }

      var result = main.compareServices(svc1, svc2);

      expect(result).equal(true)
    });
  });
});

Why is it saying it is not a function when it is?

Upvotes: 0

Views: 1392

Answers (2)

DinhNguyen
DinhNguyen

Reputation: 356

You change your code for main.js as below:

function compareServices(service1, service2) {
    if (service1.name != service2.name) {
        return false
    }
    if (service1.port != service2.port) {
        return false
    }
    if (service1.target != service2.target) {
        return false
    }
    if (service1.address != service2.address) {
        return false
    }

    return true
}

to

// if you use case => In test file you need to declare var main = new Main();
// with Main = require(... + '/main.js');
var Main = function(){};

Main.prototype.compareServices = function(service1, service2) {
    if (service1.name != service2.name) {
        return false
    }
    if (service1.port != service2.port) {
        return false
    }
    if (service1.target != service2.target) {
        return false
    }
    if (service1.address != service2.address) {
        return false
    }

    return true
}
module.exports = Main;

Or

var Main = function(){};

Main.compareServices = function(service1, service2) {
    if (service1.name != service2.name) {
        return false
    }
    if (service1.port != service2.port) {
        return false
    }
    if (service1.target != service2.target) {
        return false
    }
    if (service1.address != service2.address) {
        return false
    }

    return true
}

Upvotes: 0

Aaron Harrington
Aaron Harrington

Reputation: 532

Make sure to have the following at the end of your main.js file:

module.exports = {
  compareServices: compareServices
};

This will make the function accessible to other javascript files that require it.

Upvotes: 1

Related Questions