Search code examples
ember.jsember-cliember-testing

Ember- integration test case on action


In ember controller

action:function(){
  a:function(){
   ....
   this.set('b',true);  
  }
}

I just want to write a test case for this

test('a - function test case', function(assert) {
  var controller= this.subject();
  controller._action().a();
  assert(controller.get(b),true);
});

but this not working I'm getting undefined error.

any other way to pass this test case?


Solution

  • Looking to your code, I believe you're trying to use ember actions, if so you have to use actions: { ... } instead of action: function() { ... }.

    And to trigger an action you use the send method.

    This is an example on how to test an action in ember-cli:

    app/controllers/index

    import Ember from 'ember';
    
    export default Ember.Controller.extend({
      value: null,
      actions: {
        changeValue: function() {
          this.set('value', true);
        }
      }
    });
    

    tests/unit/controllers/index-test.js

    import {
      moduleFor,
      test
    } from 'ember-qunit';
    
    moduleFor('controller:index', {});
    
    test('it exists', function(assert) {
      var controller = this.subject();
      assert.ok(!controller.get('value'));
      controller.send('changeValue');
      assert.ok(controller.get('value'));
    });