如何在Ember-Cli中对测试类方法进行单元化

时间:2014-12-23 23:55:46

标签: javascript ember.js ember-cli ember-qunit

在我的应用程序中,我想在我的GooglePlaceRetriever服务上测试一个类方法。我知道在Ember-Cli中测试时我可以使用this.subject()获取服务的实例,但我不确定如何访问实际的GooglePlaceRetriever对象本身来测试我的类方法。

import {
  moduleFor,
  test
} from 'ember-qunit';

moduleFor('service:google-place-retriever', 'GooglePlaceRetrieverService', {
  // Specify the other units that are required for this test.
  // needs: ['service:foo']
});

// Replace this with your real tests.
test('it exists', function() {
  var service = this.subject();
  ok(service);
});

test('fetches place details from Google and converts it to a location', function()
  //Cannot figure out how to access the Class as opposed to the instance.
  var location = ???.findByPlaceId('ChIJaeKPQD_zz4kRy4c8TvnwGIg');

  ok(location.get('name'), "My Name");
});

1 个答案:

答案 0 :(得分:1)

您可以像这样访问类方法进行测试:

var location = this.subject.constructor.findByPlaceId('whatever');

为什么选择类方法呢?该服务不应该被实例化吗?