Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用JsTestDriver测试AngularJS控制器?_Javascript_Angularjs_Js Test Driver - Fatal编程技术网

Javascript 如何使用JsTestDriver测试AngularJS控制器?

Javascript 如何使用JsTestDriver测试AngularJS控制器?,javascript,angularjs,js-test-driver,Javascript,Angularjs,Js Test Driver,我有以下代码: function TestStats($xhr) { $xhr( 'GET', '/test-dashboard/get-projects.json', angular.bind(this, function(code, response) { this.projects = response.projects; this.project

我有以下代码:

function TestStats($xhr) {
    $xhr(
            'GET',
            '/test-dashboard/get-projects.json',
            angular.bind(this, function(code, response) {
                this.projects = response.projects;
                this.projects.splice(0, 0, undefined);
            }));

    this.$watch('project', angular.bind(this, function() {
        this.testClassStats = undefined;

        if (this.project) {
            $xhr(
                    'GET',
                    '/test-dashboard/get-test-stats.json?project=' + this.project,
                    angular.bind(this, function(code, response) {
                        this.testClassStats = response.testClassStats;
                    }));
        }
    }));
};

TestStats.prototype.greet = function(name) {
  return "Hello " + name + "!";
};

TestStats.$inject = ['$xhr'];
以及以下测试:

TestDashboardUnitTest = TestCase("TestDashboardUnitTest");

TestDashboardUnitTest.prototype.testAoeu = function() {
    var xhrStub = function(method, url, callback) {
    };
    var testStats = new TestStats(xhrStub);
    assertEquals("Hello World!", testStats.greet("Aoeu"));
};
以及以下配置:

server: http://localhost:9876

load:
  - http://code.jquery.com/jquery-1.6.2.min.js
  - http://code.angularjs.org/angular-0.9.17.min.js
  - web/*.js
  - test/*.js
运行测试时,JsTestDriver输出:

Total 1 tests (Passed: 0; Fails: 0; Errors: 1) (0.00 ms)
  Chrome 13.0.782.112 Linux: Run 1 tests (Passed: 0; Fails: 0; Errors 1) (0.00 ms)
    TestDashboardUnitTest.testAoeu error (0.00 ms): TypeError: Object #<TestStats> has no method '$watch'
      TypeError: Object #<TestStats> has no method '$watch'
          at new TestStats (http://127.0.0.1:9876/test/web/test-dashboard.js:13:10)
          at [object Object].testAoeu (http://127.0.0.1:9876/test/test/test-dashboard-unit-test.js:9:21)

Tests failed: Tests failed. See log for details.
总共1次测试(通过:0;失败:0;错误:1)(0.00毫秒)
Chrome 13.0.782.112 Linux:运行1测试(通过:0;失败:0;错误1)(0.00毫秒)
TestDashboardUnitTest.testAoeu错误(0.00毫秒):类型错误:对象#没有方法“$watch”
TypeError:对象#没有方法“$watch”
在新的TestStats上(http://127.0.0.1:9876/test/web/test-dashboard.js:13:10)
在[object](http://127.0.0.1:9876/test/test/test-仪表板单元测试.js:9:21)
测试失败:测试失败。有关详细信息,请参阅日志。

我需要做些什么来解决这个问题?

我不确定Angular做了什么,但在我看来,您的问题在于这段代码:

this.$watch('project', angular.bind(this, function() {
    // snip
}));
调用
this.$watch
方法时,调用的是类的对象实例的方法
TestStats
——您提供的第一个代码块中描述的类。我在任何地方都看不到名为
$watch
的方法,可能您需要其他对象引用,而不是
this

不要做平常的事,假装通过。相反,让喷射系统来完成这项工作


另外,请确保在jsTestDriver.conf文件中加载angular-mocks.js。

是的,这是测试控制器的正确方法,它使用的是scope的api,如
$watch
“此外,请确保在jsTestDriver.conf文件中加载angular-mocks.js。”您可以对此进行扩展吗?这个.conf文件在哪里?
scope = angular.scope();
$browser = scope.$service('$browser');

$browser.xhr.expectGET('phones/phones.json')
    .respond([{name: 'Nexus S'},
              {name: 'Motorola DROID'}]);
ctrl = scope.$new(PhoneListCtrl);