最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - 'equal' is not defined : Ember-qunit does not seem to be importing - Stack Overflow

programmeradmin5浏览0评论

It appears the Qunit test methods aren't available even though I'm pretty sure I am importing them correctly.

I get the following errors:

unit/models/friend-test.js: line 11, col 3, 'ok' is not defined.
unit/models/friend-test.js: line 17, col 3, 'equal' is not defined.
unit/models/friend-test.js: line 23, col 3, 'equal' is not defined.
unit/models/friend-test.js: line 31, col 3, 'equal' is not defined.
unit/models/friend-test.js: line 32, col 3, 'equal' is not defined.

I have this test file unit/models/friend-test:

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


moduleForModel('friend', 'Friend', {
  needs: ['model:article']
});

test('it exists', function() {
  var model = this.subject();
  ok(model);
});

test('fullName concats first and last name', function() {
  var model = this.subject({firstName: 'Syd', lastName: 'Barrett'});

  equal(model.get('fullName'), 'Syd Barrett');

  Ember.run(function() {
    model.set('firstName', 'Geddy');
  });

  equal(model.get('fullName'), 'Geddy Barrett', 'Updates fullName');
});

test('articles relationship', function() {
  var klass  = this.subject({}).constructor;

  var relationship = Ember.get(klass, 'relationshipsByName').get('articles');

  equal(relationship.key, 'articles');
  equal(relationship.kind, 'hasMany');
});

I'm working through "Ember CLI 101"

It appears the Qunit test methods aren't available even though I'm pretty sure I am importing them correctly.

I get the following errors:

unit/models/friend-test.js: line 11, col 3, 'ok' is not defined.
unit/models/friend-test.js: line 17, col 3, 'equal' is not defined.
unit/models/friend-test.js: line 23, col 3, 'equal' is not defined.
unit/models/friend-test.js: line 31, col 3, 'equal' is not defined.
unit/models/friend-test.js: line 32, col 3, 'equal' is not defined.

I have this test file unit/models/friend-test:

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


moduleForModel('friend', 'Friend', {
  needs: ['model:article']
});

test('it exists', function() {
  var model = this.subject();
  ok(model);
});

test('fullName concats first and last name', function() {
  var model = this.subject({firstName: 'Syd', lastName: 'Barrett'});

  equal(model.get('fullName'), 'Syd Barrett');

  Ember.run(function() {
    model.set('firstName', 'Geddy');
  });

  equal(model.get('fullName'), 'Geddy Barrett', 'Updates fullName');
});

test('articles relationship', function() {
  var klass  = this.subject({}).constructor;

  var relationship = Ember.get(klass, 'relationshipsByName').get('articles');

  equal(relationship.key, 'articles');
  equal(relationship.kind, 'hasMany');
});

I'm working through "Ember CLI 101"

Share Improve this question asked Feb 24, 2015 at 17:29 steve_gallaghersteve_gallagher 3,9088 gold badges34 silver badges52 bronze badges 5
  • Seems like it'd have to be test.ok, test.equal, etc, but I'm just guessing from first principles because I'm unfamiliar with Ember (and qunit). – Pointy Commented Feb 24, 2015 at 17:31
  • @Pointy Prefixing with test did clear the errors. Thanks. The example code doesn't include these prefixes, though, I'm curious how they could omit them. Thank you, though, at least I can proceed. – steve_gallagher Commented Feb 24, 2015 at 17:37
  • Actually, although the errors mentioned above clear after prefixing the method calls with test, the test still fails, albeit with a different message: ember_qunit.test.ok is not a function – steve_gallagher Commented Feb 24, 2015 at 17:43
  • Well all I (think I) know is that an import like that can't make global symbols like "ok" and "equals" just appear in the namespace. – Pointy Commented Feb 24, 2015 at 17:54
  • Yeah that makes sense. Still figuring this framework out. – steve_gallagher Commented Feb 24, 2015 at 18:00
Add a ment  | 

2 Answers 2

Reset to default 10

Author here! Sorry about it, I actually need to update the code since on the latest release the syntax for tests changed to match the uping version of QUNit.

Now to use: equal, ok and the other QUnit's assertions, we have to do it through a param called assert in the callback function passed to test: test('foo', function(assert){ assert.ok(true) }. I'll send a book update tonight to fix this :), in the meantime, the following should work:

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


moduleForModel('friend', 'Friend', {
  needs: ['model:article']
});

test('it exists', function(assert) {
  var model = this.subject();
  assert.ok(model);
});

test('fullName concats first and last name', function(assert) {
  var model = this.subject({firstName: 'Syd', lastName: 'Barrett'});

  equal(model.get('fullName'), 'Syd Barrett');

  Ember.run(function(assert) {
    model.set('firstName', 'Geddy');
  });

  assert.equal(model.get('fullName'), 'Geddy Barrett', 'Updates fullName');
});

test('articles relationship', function(assert) {
  var klass  = this.subject({}).constructor;

  var relationship = Ember.get(klass, 'relationshipsByName').get('articles');

  assert.equal(relationship.key, 'articles');
  assert.equal(relationship.kind, 'hasMany');
});

Look in tests/helpers/start-app.js. You should see something like:

Ember.run(function() {
    registerAcceptanceTestHelpers();
    application = Application.create(attributes);
    application.setupForTesting();
    application.injectTestHelpers();
  });

This injects the test helpers into the application global scope.

发布评论

评论列表(0)

  1. 暂无评论