Architecture: are utils good for repeatable logic?

For example, to use util, we import it like this:

import Route from 'ember-route';
import someUtil from 'app/utils/some-util.js';

export default Route.extend({
});

Using sinon.js, I’m not sure how I can stub that someUtil() method since it’s not inside the Route object. So as a workaround, I create an alias that just calls someUtil() so I can stub it. Like this:

import Route from 'ember-route';
import someUtil from 'app/utils/some-util.js';

export default Route.extend({
  someUtilAlias() {
    someUtil();
  }
});

Now in my unit test, I can simply stub the alias:

import sinon from 'sinon';

test('it works', function(assert) {
  let stub = sinon.stub().returns(true);
  let route = this.subject({someUtilAlias: stub});

  assert.ok(stub);
});
1 Like