使用茉莉花和放肆到单元测试AngularJs控制器

使用茉莉花和放肆到单元测试AngularJs控制器

本文介绍了使用茉莉花和放肆到单元测试AngularJs控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用VS2013编程,并想使我的AngularJs控制器单元测试。比如我有一个taController.js,看起来像:

I am programming with VS2013 and would like to make unit tests on my AngularJs controllers. For example I have a taController.js that looks like:

var module = angular.module("MyApp", []);

var TAController = ["$scope",
function ($scope) {
    $scope.myNumber = 2;
    $scope.add = function (number) {
        $scope.myNumber = $scope.myNumber + number;
    };
}];

和消耗这看起来像一个HTML页面:

And an HTML page that consumes this that looks like:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" data-ng-app="MyApp">
<head>
    <title></title>
    <script src="Scripts/angular.js"></script>
    <script src="Scripts/taController.js"></script>
</head>
<body>
    <div id="myDiv" data-ng-controller="TAController">
        {{myNumber}}<br />
        <a href="" ng-click="add(2)">Add 2</a><br />
    </div>
</body>
</html>

我要创建一个使用茉莉花和放肆单元测试。我创建了我的测试项目的规格目录中的AngularTest.js看起来像这样

I want to create a unit test using Jasmine and Chutzpah. I created an AngularTest.js in the specs directory of my test project that looks like this

/// <reference path="../scripts/jasmine.js" />
/// <reference path="../../unittestingwithjasmine/scripts/tacontroller.js" />

describe("My Tacontroller", function () {
    var element;
    var myScope;

    beforeEach(inject(function($scope) {
        myScope = $scope;
    }));

    it("should be able to add 2 plus 2", function () {
        myScope.add(2)
        expect(myScope.myNumber).toBe(4);
    });
});

我觉得有一些在上面code错误。其中第一幸福的
测试失败 - 我Tacontrooler遇到声明例外。消息:RefferenceError:找不到cariable:注入文件:///C....../specs/angulaertest.js(10号线)

I think there are a number of mistakes in the above code. The first of which beingTest Failed – My Tacontrooler encountered a declaration exception. Message: RefferenceError: Can’t find cariable: inject in file:///C....../specs/angulaertest.js (line 10)

我的问题是我怎么能写我的AngularTest.js正确地通过附加功能在我的测试Tacontroller

My question is how can I write my AngularTest.js to correctly test by add function on my Tacontroller

推荐答案

的错误是,我需要包括角度和角嘲笑。此外,我需要得到从根范围内的范围。下面code工作

The mistake was that I needed to include angular and angular-mocks. Also I needed to get the scope from the root scope. The following code worked

/// <reference path="../scripts/jasmine.js" />
/// <reference path="../scripts/angular.js" />
/// <reference path="../scripts/angular-mocks.js" />

/// <reference path="../../unittestingwithjasmine/scripts/tacontroller.js" />

describe("My Tacontroller", function () {

    var myScope;

    beforeEach(inject(function($rootScope, $httpBackend, $controller) {

        myScope = $rootScope.$new();

        $controller('TAController', { $scope: myScope});

    }));

    it("should be able to add 2 plus 2", function () {
        myScope.add(2);
        expect(myScope.myNumber).toBe(4);
    });
});

我自认为证明这一点,如何把它带到下一步的

我希望这可以帮助别人...

I hope this helps others...

这篇关于使用茉莉花和放肆到单元测试AngularJs控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 01:39