简体   繁体   中英

ember unit test route - this is undefined

I try to write my first unit test for a route.

project/files

actions: {
  afterSave(savedFile){
    // ... some code
    let controller = this.controllerFor('project.files');
    // ...
  }
}

the test:

test('save file', function(assert) {
  let route = this.subject();
  console.log(route);
  let project;
  Ember.run(() => {
    project = route.get('store').createRecord('project', {
      id: '1',
      name: 'test'
    });

    let afterSave = route.get('actions.afterSave');
    afterSave(project);
  });  

  assert.ok(true);
})

The problem that I am getting TypeError: Cannot read property 'controllerFor' of undefined . It looks like this is undefined.

If you have a look at Testing Routes section from Ember Guides, you can see its suggestion is to separate the action and the function. I can suggest it. It uses send method of routes, such as: route.send('afterSave');

But if you only want to make run your code, call afterSave action from your test code such as: afterSave.bind(route)(project); . Ref: bind function (I don't suggest this. Also I don't suggest you to retrieve action such as: route.get('actions.afterSave') )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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