如何使用Jasmine测试AngularJS指令?

时间:2016-05-18 09:59:59

标签: javascript angularjs unit-testing coffeescript jasmine

我只是寻找所有教程,但不明白为什么我的服务不遵循指令。

请帮我测试一下这个指令:

angular.module('app.directives').directive 'metaForPrerender', [
  'MetaFactory'
  '$compile'
  (MetaFactory, $compile) ->

    restrict: "EA",
    link: (scope) ->
      if MetaFactory.statusCode
        html = '<meta content="{{statusMetaCode}}"></meta>'
        if MetaFactory.statusCode == '301'
          html += '<meta content="{{statusMetaLocation}}"></meta>'
        angular.element('head').append($compile(html)(scope));
        scope.statusMetaCode = MetaFactory.statusCode
        scope.statusMetaLocation = MetaFactory.statusLocation

]

我的服务:

angular.module('app.services').factory 'MetaFactory', ->
  statusCode: "",
  statusLocation: ""

这是我的Jasmine测试:

element = null
scope = null

beforeEach(module('app.services'))

beforeEach inject ($rootScope, $compile) ->
  scope = $rootScope.$new()
  element = angular.element(
    '<head meta_for_prerender></head>')
  $compile(element)(scope)

describe 'MetaFactory', ->
  it 'properly injected', inject((MetaFactory) ->
    expect(MetaFactory.statusCode).toBe('')
    expect(MetaFactory.statusLocation).toBe('')
  )

describe 'metaForPrerender', ->
  it 'get right scope in directive', inject((MetaFactory) ->
    MetaFactory.statusCode = 301
    MetaFactory.statusLocation = 'NewYork'

    scope.$digest()

    expect(element.text()).toBe('<meta content="301"></meta><meta content="NewYork"></meta>')
  )

正如您所看到我的服务注入良好但看起来像指令忽略此服务和变量。

0 个答案:

没有答案