【发布时间】:2014-01-15 13:45:57
【问题描述】:
我正在尝试在不依赖我的应用程序模块的情况下对服务进行单元测试。这应该如何工作?
代码:
#karma.conf.coffee
files: [
'bower_components/angular/angular.js' # angular core
'bower_components/**/angular-*.js' # all angular plugins
'utils/typometer.js'
'utils/typometer.spec.js'
]
# utils/typometer.coffee
angular.module('myApp').factory 'typometer', ->
@sizes =
'A':8
'B':9
@ # return `this`
# utils/typometer.spec.coffee
describe 'typometer', ->
typometer = undefined # scope control
# Create a mock module so that the typometer factory has something to attach to
beforeEach angular.mock.module 'myApp' #TODO: why doesn't mock.module work?
# Inject the typometer service
beforeEach angular.mock.inject (_typometer_) ->
typometer = _typometer_ # http://docs.angularjs.org/api/angular.mock.inject
it 'exists', ->
expect(typometer).toBeDefined() # fail. 'Uncaught Error: [$injector:nomod] Module 'coatue' is not available!'
expect(typometer.sizes.A).toEqual 8
最终目标是能够实例化typometer 服务并单独测试它,而无需建立myApp 模块的真实实例。
我认为这是某种操作顺序问题。声明打字机工厂时,该模块不存在-尽管我想测试我的生产文件。在加载任何被测文件之前,我是否必须创建一个 shell 应用程序或加载真实的应用程序?这是我想从测试考虑中删除的依赖项。
【问题讨论】:
-
你想做什么来明确需要访问模块本身?
-
我不需要访问该模块,但我希望我的工厂能够自行创建。更新为使用示例的实际预期用途。
标签: angularjs coffeescript jasmine karma-runner