【发布时间】:2014-11-15 07:29:00
【问题描述】:
我的 Angular 脚本如下所示:
var app = angular.module("myApp", []);
app.controller('TestController', function ($scope) {
$scope.test= "TEST";
});
我的测试文件如下所示:
describe('first test', function() {
var $scope;
beforeEach(function (){
module('myApp');
inject(function($rootScope, $controller) {
$scope = $rootScope.$new();
ctrl = $controller('TestController', {
$scope: $scope
});
});
it('scope should have test', function() {
expect($scope.test).toEqual("TEST");
});
});
此测试失败,提示 $scope.test 未定义。我在 Chrome 中调试,发现 $scope 上有一堆属性,但没有一个是测试的。我在网上浏览了几个示例,它们看起来都与此非常相似。我不太确定我在这里做错了什么,我被卡住了....
编辑 我尝试添加 $controller 来注入,但我仍然遇到同样的问题。
【问题讨论】:
-
你认为你会在哪里得到
$controller?
标签: javascript angularjs unit-testing jasmine