givanse
givanse

Reputation: 14943

How do I unit test a helper that uses a service?

I'm trying to unit test a helper that uses a service.

This is how I inject the service:

export function initialize(container, application) {
  application.inject('view', 'foobarService', 'service:foobar');
}

The helper:

export function someHelper(input) {
  return this.foobarService.doSomeProcessing(input);
}

export default Ember.Handlebars.makeBoundHelper(someHelper);

Everything works until here.

The unit test doesn't know about the service and fails. I tried to:

test('it works', function(assert) {

  var mockView = {
    foobarService: {
      doSomeProcessing: function(data) {
          return "mock result";
      }
    }
  };

  // didn't work
  var result = someHelper.call(mockView, 42);

  assert.ok(result);
});

The error:

Died on test #1     at http://localhost:4200/assets/dummy.js:498:9
at requireModule (http://localhost:4200/assets/vendor.js:79:29)

TypeError: undefined is not a function

Upvotes: 1

Views: 187

Answers (1)

givanse
givanse

Reputation: 14943

Everything is correct, the only change needed was:

var result = someHelper.call(mockView, "42");

Upvotes: 1

Related Questions