我是单元测试概念的新手,我正在尝试设置一个简单的测试。我正在使用的平台要求所有 JavaScript 首先用 CoffeeScript 编写。我已经阅读了很多关于如何实现这一点的例子,但我的测试不断失败。
这是我的 Controller 的样子:
'use strict'
angular.module('test')
.controller 'BasicController', ['$scope', '$location', ($scope, $location) ->
$scope.page.title = "Page Title"
$scope.$watch(
() ->
return $location.url()
)
]
这是我的测试脚本的样子:
'use strict'
describe "The Basic Controller", ->
beforeEach(module('test'))
scope = {}
location = {}
ctrl = {}
beforeEach inject ($rootScope, $controller, $location) ->
scope = $rootScope.$new()
location = $location
ctrl = $controller("BasicController", {$scope: scope})
it "should have the correct title", ->
expect(scope.page.title).toBe("Page Title")
我正在使用 PhantomJS,我收到的错误消息是 scope.page.title 对象未定义。我不明白为什么会这样。我将该变量设置为测试正在检查的值,但我很困惑为什么它没有通过。任何帮助或指导将不胜感激。我不知道我是否在我的 CoffeeScript 语法中犯了错误(宁愿用 JavaScript 编码)或者我是否完全忘记了一些东西。
最佳答案
如上所述:
Figured it out...the page object that I was trying to reference was declared elsewhere. This example was abstracted from a chunk of a platform that multiple developers are working on. The solution was simply to set the page object to empty and the test passed successfully.
关于angularjs - 使用 CoffeeScript 和 Karma 对 AngularJS Controller 进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17281812/