0
votes

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?

2
Are you trying to use ember actions? If so, you have to use actions: { ... } instead of action: function() {} - Marcio Junior

2 Answers

0
votes

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'));
});
0
votes

This was working for me

test('it exists', function(assert) {
  var controller = this.subject();
  assert.ok(!controller.get('value'));
   Ember.run(function(){  
     controller.send('changeValue');
      assert.ok(controller.get('value'));
   });
});