简体   繁体   English

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

[英]How can I unit test class methods in Ember-Cli

In my application I'd like to test a class method on my GooglePlaceRetriever service. 在我的应用程序中,我想在我的GooglePlaceRetriever服务上测试一个类方法。 I know that when testing in Ember-Cli I can use this.subject() to get an instance of the service, but I'm not sure how I can access the actual GooglePlaceRetriever object, itself, to test my class methods. 我知道在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");
});

You can access class methods for testing like this: 您可以像这样访问类方法进行测试:

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

Why class methods, though? 为什么要使用类方法? Shouldn't the service be instantiated? 服务不应该实例化吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM