简体   繁体   English

在控制器内测试Ember.js路由导航

[英]Testing Ember.js route navigation from within a controller

I am attempting to write a test in my ember-cli application that tests whether a particular controller action will take the user to a specific route based on the value of an input on the page. 我试图在我的ember-cli应用程序中编写一个测试,该应用程序测试特定控制器操作是否会根据页面上输入的值将用户带到特定路径。

Here's the error I'm getting: 这是我得到的错误:

IndexController: the setTable action navigates to the tables route
    ✘ Died on test #1     at http://localhost:7357/assets/qunit.js:425
        at test (http://localhost:7357/assets/vendor.js:72425)
        at :29
        at http://localhost:7357/assets/vendor.js:54
        at http://localhost:7357/assets/test-loader.js:14: 'null' is not an object (evaluating 'target.transitionToRoute')

If I attempt it in the browser this is the error I get in the console: 如果我在浏览器中尝试它,这是我在控制台中遇到的错误:

Error while loading route: undefined

My route is defined, here's my router: 我的路线已定义,这是我的路由器:

import Ember from 'ember';

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

Router.map(function() {
  this.route('table', { path: '/table/:table_id' });
});

export default Router;

Here's my test: 这是我的测试:

import { test, moduleFor } from 'ember-qunit';

moduleFor('controller:index', 'IndexController', {
  // Specify the other units that are required for this test.
  // needs: ['controller:foo']
});

test('the setTable action navigates to the tables route', function() {
  var controller = this.subject();
  controller.set('tableNumber', 1);
  controller.send('setTable');
  ok(controller.get('currentPath') === '/table/1');
});

Here's my template: 这是我的模板:

<div class="start">
  <div class="row">
    <div class="col-xs-12">

      {{input type="text"
        valueBinding="tableNumber"
        class="form-control input-lg"
        placeholder="Enter Table #"}}

      <button {{ action 'setTable' }}
         id="submit-table"
         class="btn btn-success">Ok</button>

    </div>
  </div>
</div>

And here's my controller: 这是我的控制器:

import Ember from 'ember';

export default Ember.Controller.extend({
  tableNumber: null,

  actions: {
    setTable: function() {
      var tableNumber = this.get('tableNumber');
      console.log(this.transitionTo);
      this.transitionToRoute('table/' + tableNumber);
    }
  }
});

I also have a table controller created, it's currently empty though. 我也创建了一个表控制器,但它目前是空的。

Any help greatly appreciated! 任何帮助非常感谢!

transitionTo only exists on items extending Ember.Route . transitionTo仅存在于扩展Ember.Route项目上。

For items extending Ember.Controller / Ember.ObjectController / Ember.ArrayController transitionToRoute should be used. 对于扩展Ember.Controller / Ember.ObjectController / Ember.ArrayController项目,应使用transitionToRoute

Additionally currentPath only exists on the ApplicationController and while integration testing. 另外, currentPath仅存在于ApplicationController和集成测试中。 It looks like you're mixing integration testing and unit testing. 看起来你正在混合集成测试和单元测试。

With an Example: http://emberjs.jsbin.com/wipo/33/edit 以示例: http//emberjs.jsbin.com/wipo/33/edit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM