Injecting helper classes

Hi,

I have an existing EmberJS code base onto which I want to introduce some unit tests but I need to mock out some included files. In the example below, how can I inject the HelperClass into the unit tests subject. At the moment the subject can’t instantiate because the actual HelperClass pulls in a ton of stuff.

I am thinking that I can inject the class in with the “needs” property but I can’t figure out how :frowning:

Thanks Rob

app/models/my-model.js

import DS from 'ember-data';
import HelperClass from '../../utils/helper-class';
let model = DS.Model.extend({
    Title: DS.attr('string'),
});
HelperClass.something({ model: model });
export default model;

tests/unit/models/my-model-test.js

import { moduleForModel, test } from 'ember-qunit';
moduleForModel('my-model', 'Unit | Model | my model', {
	// Specify the other units that are required for this test.
	needs: [],
});
test('it exists', function(assert) {

    // This throws an error !!!!
	var model = this.subject();
	assert.ok(!!model);
});
1 Like

You should have access to stub any function/service/etc using the registry

Here is how I would stub a helper func to do nothing (from inside an integration test/maybe a unit test)

this.registry.register('helper:foobar', function() {});

1 Like