So I m new to the whole testing thing (I ve been one of those people who has said I should write unit tests... but never ended up ever doing it :-p). I m now writing unit tests for this project. I m using testacular + Jasmine, with browserify to compile things. I was having no problems until I started trying to do a lot AngularJS injection-stuff.
现在我只想做个NG模型的测试 来让我头脑清醒
我有一个睾丸.conf文件 包含所有必要的内容:
files = [
../lib/jquery.js ,
../lib/angular.js ,
./lib/jasmine.js ,
./lib/angular-mocks.js ,
JASMINE_ADAPTER,
./tests.js //compiled by browserify
];
我有我的控制器定义(MainCtrl.coffee)
MainCtrl = ($scope, $rootScope) ->
$scope.hello = initial
module.exports = (angularModule) ->
angularModule.controller MainCtrl , [ $scope , $rootScope , MainCtrl]
return MainCtrl
我有自己的测试: (MainCtrlTest. coffee, 和MainCtrl.coffee同名)
testModule = angular.module MainCtrlTest , []
MainCtrl = require( ./MainCtrl )(testModule)
describe MainCtrlTest , ->
scope = null
elm = null
ctrl = null
beforeEach inject ($rootScope, $compile, $controller) ->
scope = $rootScope.$new()
ctrl = $controller MainCtrl, $scope: scope
elm = $compile( <input ng-model="hello"/> )(scope)
describe value $scope.hello , ->
it should initially equal input value , ->
expect(elm.val()).toBe scope.hello
it should change when input value changes , ->
scope.$apply -> elm.val( changedValue )
expect(scope.hello).toBe elm.val()
测试立即失败, 输入 s elm. val () 返回空白, 范围. hello 返回预期值( 初始值, 设置在 MainCtrl. coffee)
我在这里做错什么了?