模拟余烬回购附加服务

时间:2018-10-26 10:57:38

标签: ember.js ember-cli ember-testing ember-addon

我有一个回购附件:

/my-app/lib/my-service/app/services/my-service.js

import AjaxService from 'ember-ajax/services/ajax';
import ENV from '../config/environment';

export default AjaxService.extend({
    host: ENV.APP.BACKEND_URL,
    contentType: 'application/json; charset=utf-8',

    subscribe(emailAddress) {
       ...
       return this.request(url, {method: 'POST', data: data});
    }

我有一个使用此in-repo-addon的主要应用程序组件。

/my-app/app/components/my-comp.js

import Ember from "ember";

export default Ember.Component.extend({
   myService: Ember.inject.service(),
   ...
});

并且在应用程序正常运行时有效。我也对该组件进行了测试。

/my-app/tests/integration/components/my-comp-test.js

import { moduleForComponent, test } from 'ember-qunit';
import ENV from 'my-app/config/environment';

moduleForComponent('sign-up', 'Integration | Component | my comp', {
    integration: true,
});

test('email addresses are trimmed before submission to remote service', function (assert) {
  const signupEmail = " NotAnEmail@example.com ";

  let myService = Ember.Service.extend({

    subscribeUrl() {
      return "";
    },

    request(url, payload) {
      assert.equal("NotAnEmail@example.com", payload.data.emailAddress);
      return new Ember.RSVP.Promise((res) => {
        res({message: 'success'});
      });
    }
  });
console.log("myService=", myService);
this.register('service:my-service', myService);
...

该测试尝试创建myService并将其扩展为覆盖/模拟各种位以测试断言,以确保已修剪电子邮件地址。

但是myService始终是null。看起来像Ember.Service在构建测试时不包含in-repo-addons,因此永远找不到myService。

我尝试使用以下内容直接导入服务,但ember也不喜欢:

import myService from 'my-app/lib/my-service/app/services/my-service/my-service';
// or
import myService from 'my-app/lib/my-service/app/services/my-service';

这是Ember测试的错误吗?我该如何解决?

0 个答案:

没有答案