angularjs - 使用 CoffeeScript 和 Karma 对 AngularJS Controller 进行单元测试

标签 angularjs testing coffeescript phantomjs karma-runner

我是单元测试概念的新手,我正在尝试设置一个简单的测试。我正在使用的平台要求所有 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/

相关文章:

javascript - 为什么我的 jsp 页面无法识别我的 Angular Controller ?

c++ - Googletest 不运行测试夹具

javascript - 为什么在 javascript 中,一个自调用的匿名函数会包围 jQuery onReady 回调?

inheritance - CoffeeScript,实现 'implements'

javascript - 如何通过与 Controller 不同的方式加载 ui.bootstrap 模块?

javascript - Angular js - 使用延迟加载从 View 中需要 js 文件

angularjs - 如何分析 AngularJS 组件中的性能基准?

javascript - 开 Jest - TypeError : Cannot read property 'fn' of undefined

javascript - onClick 的模拟 SyntheticEvent 处理程序

Javascript/Coffeescript 当值相同时对多个键上的对象数组进行排序