mardi 12 avril 2016

Method looks up a property file for a value, how to test method while providing fake values to the property file?

container.js has a function called getService().

getService() checks CONFIG to get value for store.

I'd like to write 2 unit tests, 1 to test service is instanciated if store property is set and another to check an Error is created if store property is not set.

Do I need to create a mock of container.js in order to provide my own CONFIG values?

This is container.unit.test.js ..

var should = require('should');
var proxyquire = require('proxyquire');
var sinon = require('sinon');
var container = require('../src/container.js');

describe('container', function() {

  before(function() { });
  after(function() { });

  it('should throw error since CONFIG.store property is not configured', function() {
    var caughtError;
    try {
      container.getService();
    } catch (err) {
      caughtError = err;
    }
    should.exist(caughtError);
    caughtError = null;
  });

  it('should construct service since CONFIG.store property is configured', function() {
    var service = container.getService();
    should.exist(service);
  });
});

This is container.js ..

var AnotherService = require('./anotherService');
var CONFIG = require('./config');

module.exports.getService = function() {
  var service;
  if (CONFIG.store === 'somevalue') {
    service = new AnotherService();
  } else {
    throw new Error('store not configured correctly');
  }
  return service;
};

Aucun commentaire:

Enregistrer un commentaire