2014-03-28 45 views
0

我有一個AngularJS應用程序。我想實施一些我可以按需運行的端到端測試。在努力做到這一點,我已經建立了一個基本的測試屏幕如下:茉莉花端對端測試AngularJS應用程序

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>Test Results</title> 

    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jasmine/2.0.0/jasmine.js"></script> 
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jasmine/2.0.0/jasmine-html.js"></script> 
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jasmine/2.0.0/boot.js"></script> 

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script> 
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular-mocks.js"></script> 

    <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/jasmine/2.0.0/jasmine.css" /> 

    <!-- Load the Test Files--> 
    <script type="text/javascript" src="e2e/tests.e2e.js"></script> 
</head> 
<body> 
    <a href="#" onclick="env.execute()">run tests</a> 

</body> 
</html> 

我tests.e2e.js文件如下所示:

'use strict'; 

describe('MyApp', function() { 
    browser.get('http://localhost:11000/index.html'); 

    describe('Welcome Screen', function() { 
    }); 
}); 

時,單擊「運行測試「在我的測試運行,我得到一個錯誤,指出:

MyApp encountered a declaration exception 
ReferenceError: browser is not defined 

我的問題是,我究竟做錯了什麼?我見過的例子使用瀏覽器來啓動應用程序。但是,我似乎無法弄清楚如何按需進行端到端測試。

感謝您提供任何幫助。

+0

的可能重複的[哪裏瀏覽器()對象被在angluarJS定義?](http://stackoverflow.com/questions/13754336/where-does-the-browser-object- is-defined-in-angluarjs) – Conqueror

+0

即使在包含angular-scenario.js後,我仍然得到「瀏覽器未定義」的錯誤。 – user3284007

+0

你的測試跑步者是什麼? – andrbmgi

回答

0
(function (module) { 

    var myController = function ($scope, $http) { 

     $http.get("/api/myData") 
      .then(function (result) { 
       $scope.data= result.data; 
      }); 
    }; 

    module.controller("MyController", 
     ["$scope", "$http", myController]); 

}(angular.module("myApp"))); 



describe("myApp", function() { 

    beforeEach(module('myApp')); 

    describe("MyController", function() { 

     var scope, httpBackend; 
     beforeEach(inject(function ($rootScope, $controller, $httpBackend, $http) { 
      scope = $rootScope.$new(); 
      httpBackend = $httpBackend; 
      httpBackend.when("GET", "/api/myData").respond([{}, {}, {}]); 
      $controller('MyController', { 
       $scope: scope, 
       $http: $http 
      }); 
     })); 

     it("should have 3 row", function() { 
      httpBackend.flush(); 
      expect(scope.data.length).toBe(3); 
     }); 
    }); 
}); 
+3

你能解釋你的答案嗎? :) – glepretre