最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to test AngularJS controller with JsTestDriver? - Stack Overflow

programmeradmin0浏览0评论

I have the following code:

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'];

and the following test:

TestDashboardUnitTest = TestCase("TestDashboardUnitTest");

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

and the following config:

server: http://localhost:9876

load:
  - .6.2.min.js
  - .9.17.min.js
  - web/*.js
  - test/*.js

When I run the test, JsTestDriver outputs:

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.

What do I need to do to fix this?

I have the following code:

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'];

and the following test:

TestDashboardUnitTest = TestCase("TestDashboardUnitTest");

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

and the following config:

server: http://localhost:9876

load:
  - http://code.jquery./jquery-1.6.2.min.js
  - http://code.angularjs/angular-0.9.17.min.js
  - web/*.js
  - test/*.js

When I run the test, JsTestDriver outputs:

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.

What do I need to do to fix this?

Share Improve this question edited Aug 18, 2017 at 17:27 Graham 7,81220 gold badges65 silver badges92 bronze badges asked Aug 16, 2011 at 1:22 Noel YapNoel Yap 19.8k21 gold badges105 silver badges161 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

From http://docs.angularjs/#!/tutorial/step_05

scope = angular.scope();
$browser = scope.$service('$browser');

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

Don't do the usual thing and pass in mocks. Instead, let the injection system do the work.

Also, be sure to load angular-mocks.js in the jsTestDriver.conf file.

I'm not sure what Angular does, but it seems to me that your problem is in this block of code:

this.$watch('project', angular.bind(this, function() {
    // snip
}));

When you are calling this.$watch method, you are calling a method of an object instance of a class TestStats - the class that is described in the first block of code you provided. I don't see a method called $watch anywhere there, maybe you need some other object reference, not this?

发布评论

评论列表(0)

  1. 暂无评论