端至端测试AngularJS应用茉莉 [英] End-to-End Testing AngularJS app with Jasmine

查看:108
本文介绍了端至端测试AngularJS应用茉莉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AngularJS应用。我想实现一些终端到终端的测试,我可以按需运行。在努力做到这一点,我已经建立了一个基本的测试屏幕如下:

 <!DOCTYPE HTML>
< HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
< HEAD>
    &LT;标题&GT;测试结果片剂; /标题&GT;    &LT;脚本类型=文/ JavaScript的SRC =// cdnjs.cloudflare.com/ajax/libs/jasmine/2.0.0/jasmine.js\"></script>
    &LT;脚本类型=文/ JavaScript的SRC =// cdnjs.cloudflare.com/ajax/libs/jasmine/2.0.0/jasmine-html.js\"></script>
    &LT;脚本类型=文/ JavaScript的SRC =// cdnjs.cloudflare.com/ajax/libs/jasmine/2.0.0/boot.js\"></script>    &LT;脚本类型=文/ JavaScript的SRC =htt​​ps://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js&GT;&LT; / SCRIPT&GT;
    &LT;脚本类型=文/ JavaScript的SRC =htt​​ps://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular-mocks.js&GT;&LT; / SCRIPT&GT;    &LT;链接rel =stylesheet属性HREF =// cdnjs.cloudflare.com/ajax/libs/jasmine/2.0.0/jasmine.css/&GT;    &LT;! - 加载测试文件 - &GT;
    &LT;脚本类型=文/ JavaScript的SRC =E2E / tests.e2e.js&GT;&LT; / SCRIPT&GT;
&LT; /头&GT;
&LT;身体GT;
    &LT; A HREF =#的onclick =env.execute()&gt;运行测试和LT; / A&GT;&LT; /身体GT;
&LT; / HTML&GT;

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

 使用严格的;描述('MyApp的',函数(){
    browser.get(的http://本地主机:11000 / index.html的');    描述(欢迎屏幕,函数(){
    });
});

当点击运行测试在我的测试运行,我得到一个错误,指出:

 遇到的MyApp的声明异常
的ReferenceError:浏览器没有被定义

我的问题是,我究竟做错了什么?我见过使用浏览器的例子,基本启动应用程序。不过,我似乎无法弄清楚如何做点播终端到终端的测试。

感谢您的帮助,您可以提供。


解决方案

 (函数(模块){    VAR = myController的功能($范围,$ HTTP){        $ http.get(/ API / MYDATA的)
            。然后(功能(结果){
                $ scope.data = result.data;
            });
    };    module.controller(myController的
        [$范围,$ HTTP,myController的]);}(angular.module(对myApp)));描述(对myApp功能(){    beforeEach(模块('对myApp'));    描述(myController的功能(){        VAR范围,httpBackend;
        beforeEach(注(函数($ rootScope,$控制器,$ httpBackend,$ HTTP){
            范围= $ rootScope $新的()。
            httpBackend = $ httpBackend;
            httpBackend.when(GET,/ API / myData的),响应([{},{},{}])。
            $控制器('myController的',{
                $适用范围:适用范围,
                $ HTTP:$ HTTP
            });
        }));        它(应该有3排功能(){
            httpBackend.flush();
            期待(scope.data.length).toBe(3);
        });
    });
});


I have an AngularJS app. I would like to implement some end-to-end testing that I can run on-demand. In an effort to do this, I've built a basic test screen with the following:

<!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>

My tests.e2e.js file looks like the following:

'use strict';

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

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

When click "run tests" in my test runner, I get an error that says:

MyApp encountered a declaration exception
ReferenceError: browser is not defined

My question is, what am I doing wrong? The examples I've seen use browser to basically start the app. However, I can't seem to figure out how to do end-to-end tests on-demand.

Thank you for any help you can provide.

解决方案

(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);
        });
    });
});

这篇关于端至端测试AngularJS应用茉莉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆