AngularJS - basic injection testing

So, I am new to all testing (I was one of those people who said "I have to write unit tests ..." but never finished: -p). Now I am writing unit tests for this project. I am using testacular + Jasmine using a browser to compile things. I had no problems until I started making a lot of AngularJS injections.

Right now I'm just trying to run an ng model test to get around all this.

I have a testacular.conf file that includes everything you need:

files = [
    '../lib/jquery.js',
    '../lib/angular.js',
    './lib/jasmine.js',
    './lib/angular-mocks.js',
    JASMINE_ADAPTER,
    './tests.js' //compiled by browserify
];

I have my controller installed (MainCtrl.coffee)

MainCtrl = ($scope, $rootScope) ->
    $scope.hello = 'initial'

module.exports = (angularModule) ->
    angularModule.controller 'MainCtrl', ['$scope', '$rootScope', MainCtrl]
    return MainCtrl

And I have my test: (_MainCtrlTest.coffee, in the same directory as 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()

: elm.val() , scope.hello ('initial', MainCtrl.coffee)

?

+5
1

, scope.$apply():

it 'should initially equal input value', ->
  scope.$apply()
  expect(elm.val()).toBe scope.hello

,

Angular ng-model. .

:

  • ( scope.hello)
  • html ( , )

, DOM. AngularJS - /.

, :

it 'should init hello', ->
  expect(scope.hello).toBe 'initial'

( + ), e2e. , .. , . html , , AngularJS.

+10

All Articles