我有一个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
我的问题是,我做错了什么?我看到的例子基本上是使用浏览器来启动应用程序。然而,我似乎不知道如何按需进行端到端测试。
感谢您提供的任何帮助
(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); }); }); });