English 中文(简体)
角JS - 注射基本检测
原标题:AngularJS - basic testing with injection

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)

我在这里做错什么了?

最佳回答

要让此操作, 您需要做 < code> scope.$$apply () :

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

<强 > 不要测试框架, 测试您的代码

您的测试正在尝试测试角线是否绑定, 以及 ng- model 是否有效。 您应该信任框架并测试您的代码 。

您的代码是:

  1. the controller (setting initial scope.hello value)
  2. html templates (and all the binding, directives in there)

你可以很容易地测试第一个, 甚至不触动任何 DOM 。 这就是角JS 的美, 强烈的视觉/逻辑分离 。

在此控制器中, 几乎没有什么可以测试的, 但初始值 :

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

要测试第二个(模板 + 绑定), 您想要进行e2e 测试。 您基本上想要测试, 模板是否不含任何绑定中的打字符... 所以您想要测试真实的模板。 如果您在测试中插入一个不同的 html, 您只能测试 AgragleJS 。

问题回答

暂无回答




相关问题
Selenium not working with Firefox 3.x on linux

I am using selenium-server , selenium rc for UI testing in my application . My dev box is Windows with FireFox 3.5 and every thing is running fine and cool. But when i try to run selenium tests on my ...

Best browser for testing under Safari Mobile on Linux?

I have an iPhone web app I m producing on a Linux machine. What s the best browser I can use to most closely mimic the feature-limited version of Safari present on the iPhone? (It s a "slimmed down" ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Is there any error checking web app cralwers out there?

Wondering if there was some sort of crawler we could use to test and re-test everything when changes are made to the web app so we know some new change didn t error out any existing pages. Or maybe a ...