简体   繁体   中英

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. 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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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