无法在指令中使用服务方法;显示方法在服务上定义的测试

Unable to use service method in a directive; tests showing the method is defined on the service

我正在尝试制定一个指令,以我喜欢的格式生成 Lorem Ipsum 文本。这是我拥有的:

angular.module('mock.text', [])
  .factory('TextGenerator', function() {
    var words = [/** lots of array elements (lorem ipsum text) **/];
    return {
      createWords: function(wordCount) {
        var wordCount = wordCount;
        var idx = Math.floor(Math.random() * 199);
        var sentence = words.slice(idx, wordCount);
        sentence.join(' ');
        sentence.charAt(0).toUpperCase();

        return sentence;
      }
    }
  })

  .directive('text', function(TextGenerator) {
    return {
      restrict: 'EA',
      link: function(scope, element, attrs) {
        var words = TextGenerator.createWords(Number(attrs['numWords']));
        scope.words = words;
      },
      scope: {},
      template: '<p> {{words}} </p>'
    }
  });

然后发送到主应用程序模块:

angular.module('myApp', ['mock.text']);

我还有一个测试来检查服务是否正常工作:

describe('TextGenerator', function() {
    beforeEach(module('myApp'));

    it('should have a createWords method', function() {
        angular.mock.inject(function(TextGenerator) {
            expect(TextGenerator.createWords).toBeDefined();
        });
    });
});

上面的测试通过了,但是当我尝试像这样实际使用指令时

<p text num-words="7"></p>

我在控制台中收到一个错误:undefined is not a function 并且它发生的行在指令定义中

var words = TextGenerator.createWords(Number(attrs['numWords']));

我尝试了其他方法试图找到问题的根源,但没有成功。有人可以解释一下这里发生了什么吗?

服务一切都很好,我厌倦了你的代码。 代码在 sentence.charAt(0).toUpperCase(); 中断 正确复习 createWords 方法。