Note that there are some explanatory texts on larger screens.

plurals
  1. POAngularJS - basic testing with injection
    primarykey
    data
    text
    <p>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.</p> <p>Right now I'm simply trying to do a test of ng-model to get my head around all of it.</p> <p>I have a testacular.conf file which includes everything necessary:</p> <pre><code>files = [ '../lib/jquery.js', '../lib/angular.js', './lib/jasmine.js', './lib/angular-mocks.js', JASMINE_ADAPTER, './tests.js' //compiled by browserify ]; </code></pre> <p>I have my controller defined (MainCtrl.coffee)</p> <pre><code>MainCtrl = ($scope, $rootScope) -&gt; $scope.hello = 'initial' module.exports = (angularModule) -&gt; angularModule.controller 'MainCtrl', ['$scope', '$rootScope', MainCtrl] return MainCtrl </code></pre> <p>And I have my test itself: (_MainCtrlTest.coffee, in same directory as MainCtrl.coffee)</p> <pre><code>testModule = angular.module 'MainCtrlTest', [] MainCtrl = require('./MainCtrl')(testModule) describe 'MainCtrlTest', -&gt; scope = null elm = null ctrl = null beforeEach inject ($rootScope, $compile, $controller) -&gt; scope = $rootScope.$new() ctrl = $controller MainCtrl, $scope: scope elm = $compile('&lt;input ng-model="hello"/&gt;')(scope) describe 'value $scope.hello', -&gt; it 'should initially equal input value', -&gt; expect(elm.val()).toBe scope.hello it 'should change when input value changes', -&gt; scope.$apply -&gt; elm.val('changedValue') expect(scope.hello).toBe elm.val() </code></pre> <p>The test fails immediately, with the input's elm.val() returning blank, and scope.hello returning the intended value ('initial', set in MainCtrl.coffee)</p> <p>What am I doing wrong here?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload