【问题标题】:Ember Hello Word - Test Error in http://localhost:4200/tests but not in command lineEmber Hello Word - http://localhost:4200/tests 中的测试错误但命令行中没有
【发布时间】:2015-08-25 02:03:43
【问题描述】:

我正在尝试通过关注this tutorial 来构建一个 hello world Ember.js 应用程序...我已经到了测试“关于”页面链接的部分。

我的设置是:

QUnit 1.18.0; Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36
Ember version: 1.13.8
node: 0.12.7
npm: 2.13.4
os: darwin x64

我的router.js 是:

import Ember from 'ember';
import config from './config/environment';

var Router = Ember.Router.extend({
  location: config.locationType
});

Router.map(function() {
  this.route('about');
});

export default Router;

我的application.hbs 是:

<h2 id="title">Welcome to Boston Ember</h2>

{{link-to 'About' 'about'}}

{{outlet}}

我的about.hbs 是:

<h3>About</h3>

<p>Boston Ember is the monthly meetup where awesome people get together

做很棒的 Ember 相关的事情!

我的about-page-test.js 是:

import Ember from 'ember';
import startApp from 'bostonember/tests/helpers/start-app';

var App;

module('Integration - About Page', {
  beforeEach: function() {
    App = startApp();
  },
  afterEach: function() {
    Ember.run(App, 'destroy');
  }
});

test('Should navigate to the About page', function() {
  visit('/').then(function(assert) {
    click("a:contains('About')").then(function(assert) {
      assert.equal(find('h3').text(), 'About');
    });
  });
});

页面正常运行,命令行不返回任何错误:

file changed templates/application.hbs

Build successful - 691ms.

但是当我转到http://localhost:4200/tests 时,我看到一堆错误...

Integration - About Page: Should navigate to the About page (1, 0, 1)Rerun210 ms
TypeError: Cannot read property 'equal' of undefined@ 190 ms
Expected:   true
Result:     false
Diff:   trufalse
Source:     
    at http://localhost:4200/assets/test-support.js:4578:13
    at exports.default._emberTestingAdaptersAdapter.default.extend.exception (http://localhost:4200/assets/vendor.js:52460:34)
    at onerrorDefault (http://localhost:4200/assets/vendor.js:43162:24)
    at Object.exports.default.trigger (http://localhost:4200/assets/vendor.js:67346:11)
    at Promise._onerror (http://localhost:4200/assets/vendor.js:68312:22)
    at publishRejection (http://localhost:4200/assets/vendor.js:66619:15)

JSHint - integration: integration/about-page-test.js should pass jshint (1, 0, 1)Rerun1 ms
integration/about-page-test.js should pass jshint.
integration/about-page-test.js: line 6, col 1, 'module' is not defined.
integration/about-page-test.js: line 15, col 1, 'test' is not defined.
integration/about-page-test.js: line 16, col 28, 'assert' is defined but never used.

3 errors@ 0 ms
Expected:   true
Result:     false
Diff:   trufalse
Source:     
    at Object.<anonymous> (http://localhost:4200/assets/bostonember.js:337:12)
    at Object.Test.run (http://localhost:4200/assets/test-support.js:3566:28)
    at http://localhost:4200/assets/test-support.js:3695:11
    at process (http://localhost:4200/assets/test-support.js:3254:24)
    at begin (http://localhost:4200/assets/test-support.js:3299:2)
    at http://localhost:4200/assets/test-support.js:3315:4

我尝试过使用this.route('about'); 的一些不同变体,尽管我认为这很好,因为该功能有效。我也在测试中尝试了一些不同的东西,但没有任何东西可以摆脱浏览器错误。

【问题讨论】:

    标签: javascript ruby-on-rails node.js ember.js


    【解决方案1】:

    您需要包含用于断言的 qUnit 和用于 ember 测试助手的 startApp。

    import Ember from "ember";
    import { module, test } from 'qunit';
    import startApp from '../helpers/start-app';
    

    可以为模型定义模块。

    import { test, moduleForModel } from 'ember-qunit';
    
    
    moduleForModel('recipe/recipe', 'Unit | Models | Recipe |', {
        needs: ['model:recipe/recipe', 'model:recipe/category', 'model:filerepository/file']
    });
    

    在哪里

    this.subject()

    对导入模块的引用。

    示例:http://www.ember-cli.com/user-guide/#testing

    【讨论】:

    • 我添加了导入,但仍然遇到相同的错误。我也错过了我错过的 phantonJS。仍然有错误。
    【解决方案2】:

    好像我把asserts 放在了错误的地方。以下通过:

    import Ember from 'ember';
    import { module, test } from 'qunit';
    import startApp from '../helpers/start-app';
    
    var App;
    
    module('Integration - About Page', {
      beforeEach: function() {
        App = startApp();
      },
      afterEach: function() {
        Ember.run(App, 'destroy');
      }
    });
    
    test('Should click About link', function(assert) {
      visit('/').then(function() {
        click("a:contains('About')").then(function() {
          assert.equal(find('h3').text(), 'About');
        });
      });
    });
    

    【讨论】:

    • 这段代码与你第一次发布的代码几乎相同,唯一的区别是你有“import { module, test } from 'qunit';” ?
    • 问题出在断言的位置上。非常细微的差异,但它是导致上述错误之一的原因。我并不是说您的解决方案不好。只是提供完整的解决方案,因为在最近的 ember 版本更新中测试略有变化。
    猜你喜欢
    • 2018-11-15
    • 2015-06-26
    • 2017-08-19
    • 2016-11-28
    • 1970-01-01
    • 2018-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多