模拟控制器用于qunit测试。

时间:2016-06-04 00:36:11

标签: javascript unit-testing ember.js qunit ember-qunit

我有一个mixin,可帮助我完成步骤的工作流程。基本上它会查看当前步骤是什么,然后基于此做一些魔术。问题是函数的第一行抓取this.controllerFor('document'),我不明白如何模拟controllerFor调用。我知道我可以在测试中将{testing: "123"}传递给.create。这会给我this.get('testing'),但如何模仿controllerFor?另外,要清楚,controllerFor调用的唯一原因是我可以获得currentStep

制备-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...
}

制备-下一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);
});

1 个答案:

答案 0 :(得分:1)

您可以在测试前或测试中替换controllerFor方法:

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.