EmberJS Service Injection for Unit Tests (Ember QUnit)

前端 未结 1 1585
萌比男神i
萌比男神i 2021-01-17 20:20

Specs:

  • Ember version: 1.13.8
  • node: 0.10.33
  • npm: 2.13.4

I have

1条回答
  •  粉色の甜心
    2021-01-17 20:43

    You don't have to import service. You have to include service in needs like below.

    moduleFor("controller:test", {
       needs: ['service:alias']
    });
    

    For eg:

    service / alias.js

    Em.service.extend({
      name: 'john'
    });
    

    controllers / test.js

    Em.Controller.extend({
      alias: Em.service.inject(),
    
      test: function() {
        alert(this.get('alias.name');
        }
      });
    

    tests/unit/controllers/test-test.js

    moduleFor('controller:test', {
      needs: ['service:store']
    });
    
    test('Alias Alias Alias', function(assert) {
    
        var controller = this.subject();
        assert.equal(controller.get('store.name'), 'john);
    
        });
    

    For this test to run, Ember will generate a container with the controller test and service alias. So you can access the service properties with its name prefixed.

    0 讨论(0)
提交回复
热议问题