How to correctly write a unit/integration test for a service

Hi,

I want a service that converts objects in json. For now, have this test :

/* jshint expr:true */
import Ember from 'ember';
import { expect } from 'chai';
import { describeModule, it } from 'ember-mocha';
import startApp from '../../helpers/start-app';

var app, container, store;

describeModule(
  'service:create-subscriptions',
  'CreateSubscriptionsService',
  {
    needs: []
  },
  function() {
    before(function() {
      app = startApp();
      container = app.__container__;
      store = container.lookup('service:store');
    }),
    it('returns items in json format', function() {
      let service = this.subject();

      var subscription = store.createRecord('subscription', {email: 'test@test.com'});
      service.items.pushObject(subscription);

      var result = service.toJSON();
      expect(result[0].email).to.equal('test@test.com');
    });
  }
);

And this service :

import Ember from 'ember';

export default Ember.Service.extend({
  items: [],
  toJSON: function() {
    return this.get('items').map((item) => {
      return item.toJSON();
    });
  }
});

It works but I’m uncomfortable with my code because I have to lookup for the store and it seems to be a hack.

I saw on the Internet than it’s better to use Ember.Object.create but it returns an object without toJSON method. I can stub it but I will not be confident in the behavior my method.

What’s the best solution ?

I am having the same problem.

I know this is an old thread, but I feel that this is still an unsolved problem.

Does anyone know how to run unit test on services?