2011-08-16 34 views
7

Tôi có đoạn mã sau:Làm thế nào để kiểm tra bộ điều khiển AngularJS với JsTestDriver?

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

và kiểm tra sau:

TestDashboardUnitTest = TestCase("TestDashboardUnitTest"); 

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

và cấu hình như sau:

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 

Khi tôi chạy thử nghiệm, JsTestDriver kết quả đầu ra:

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. 

Tôi cần phải làm gì để sửa lỗi này?

Trả lời

5

Từ http://docs.angularjs.org/#!/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); 

Đừng làm điều thông thường và vượt qua trong mocks. Thay vào đó, hãy để hệ thống phun hoạt động.

Ngoài ra, hãy đảm bảo tải angular-mocks.js trong tệp jsTestDriver.conf.

+1

Đúng, đây là cách tiếp cận đúng để kiểm tra bộ điều khiển sử dụng api của phạm vi như '$ watch'. – Vojta

+0

"Ngoài ra, hãy nhớ tải angular-mocks.js trong tệp jsTestDriver.conf." Bạn có thể mở rộng về điều này? Tệp .conf ở đâu? – cdmckay

1

Tôi không chắc chắn những gì góc không, nhưng có vẻ như với tôi rằng vấn đề của bạn là trong khối mã này:

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

Khi bạn đang gọi this.$watch phương pháp, bạn đang gọi một phương pháp của một đối tượng thể hiện của một lớp học TestStats - lớp được mô tả trong khối mã đầu tiên bạn đã cung cấp. Tôi không thấy một phương pháp có tên là $watch ở bất kỳ đâu, có thể bạn cần một số tham chiếu đối tượng khác, không phải là this?

Các vấn đề liên quan