0

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?

Timmy O'Mahony
  • 53,000
  • 18
  • 155
  • 177

1 Answers1

2

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

Timmy O'Mahony
  • 53,000
  • 18
  • 155
  • 177
  • 1
    I wonder if you can get away with not having "moduleFor" in this case? It seems a tiny bit wasteful to have it create an instance in the background that you're never going to be using, I wonder if there is a cleaner way to test static methods without objects being instantiated at all – real_ate Mar 03 '17 at 21:23