Zack
Zack

Reputation: 2497

Mocking controllerFor in qunit tests.

I have a mixin that helps me get through a workflow of steps. Basically it looks at what the current step is and then does some magic based on that. The issue is that the first line of the function grabs this.controllerFor('document') and I don't understand how to mock that controllerFor call. I know that I can, for example, pass {testing: "123"} into the .create in the test. This will then give me this.get('testing') but how do I mock the controllerFor? Also, to be clear, the only reason for the controllerFor call is so that I can get the currentStep.

prepare-next.js

setNext: function() {
    let currentStep = this.controllerFor('document').get('currentStep');

    if (currentStep === 'First_step') {
        //do something
    } else {
        //do something else...
    }
    // Bunch more stuff happens...
}

prepare-next-test.js

import Ember from 'ember';
import PrepareNext from 'my-app/mixins/prepare-next';
import { module, test } from 'qunit';

module('mixin:prepare-next');

const PrepareNextObject = Ember.Object.extend(PrepareNext);
let subject;


test('#setNext does all the things', function(assert) {
  subject = PrepareNextObject.create();
  subject.setNext();
  //assert.equal(foo, bar);
});

Upvotes: 1

Views: 366

Answers (1)

Daniel
Daniel

Reputation: 18672

You can replace controllerFor method before or in test:

import Ember from 'ember';
import PrepareNext from '../../../mixins/prepare-next';
import { module, test } from 'qunit';

module('mixin:prepare-next');

const PrepareNextObject = Ember.Object.extend(PrepareNext);

let subject;

test('#setNext does all the things', function(assert) {
  assert.expect(2);

  subject = PrepareNextObject.create();
  subject.controllerFor = () => {
    assert.ok(true, 'controllerFor called');
    return {
      get() {
        assert.ok(true, 'Get called');
        return 'First_step';
      }
    }
  };
  subject.setNext();
  //assert.equal(foo, bar);
});

Working demo.

Upvotes: 1

Related Questions