ember.jsember-qunit

How can I test a class function with Ember qUnit?


I have a simple object (in app/models/fruit.js) that has a static method:

import Ember from 'ember';

const Fruit = Ember.Object.extend({

});

Fruit.reopenClass({
    createFruit() {
    }
}

export default Fruit;

and I have a test (in tests/unit/models/fruit-test.js):

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

moduleFor('model:fruit', 'Unit | Model | fruit', {
});

test('has static  method', function(assert) {
  let model = this.subject();
  assert.ok(model.createFruit);
});

this correctly fails because - as I understand it - the model is an actual instance of my class, not the class itself.

This is mentioned in the testing docs:

Test helpers provide us with some conveniences, such as the subject function that handles lookup and instantiation for our object under test.

as well as theember-qunit docs:

You do not have direct access to the component instance.

So how can I tests a class function/property instead of just instance methods/properties?


Solution

  • The simple answer to this is to simply import the class directly into the test file:

    import Ember from 'ember';
    import { moduleFor, test } from 'ember-qunit';
    
    import Fruit from 'myapp/models/fruit';
    
    moduleFor('model:fruit', 'Unit | Model | fruit');
    
    test('has static  method', function(assert) {
        assert.ok(Fruit.createFruit);
    });
    

    I thought that the class might be saved somewhere on this but this is a much simpler approach