使用带有phantomjs的Jasmine测试Angular http服务

时间:2017-02-14 15:42:04

标签: angularjs jasmine phantomjs

我已经创建了3个文件来测试服务器RESTAPI,并使用带有Jasmine的角度$ http服务。

当我使用CHROME浏览器加载SpecRunner.html时,测试正在运行,并且http get请求被发送到服务器并且测试验证正常。

我下载phanton for win并在Phantom中编写以下代码以加载index.html并运行它,但幻像加载SpecRunner.html并运行测试但是http get请求不会发送到服务器。

var page = require('webpage').create(), fs = require('fs'),

address = "file:///C://Users//bmp015//Desktop//jasmine-standalone//SpecRunner.html";
console.log('isFile? ' + fs.isFile(address));
console.log('isReadable? ' + fs.isReadable(address));

page.open(address, function(status){
    console.log('status? ' + status);
    console.log(page.content)

    var title = page.evaluate(function () {
        var posts = document.getElementsByClassName("jasmine-bar");
        return posts[0].innerHTML;
    });
    console.log("\n\n\n\n\n");
    console.log(title);
    phantom.exit();
    console.log("\n\n\n\n\n");
});

SpecRunner.html文件加载角度文件,jasmine独立文件和测试javascript文件(下面的文件)。

SpecRunner.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Jasmine Spec Runner v2.5.2</title>

  <link rel="shortcut icon" type="image/png" href="lib/jasmine-2.5.2/jasmine_favicon.png">
  <link rel="stylesheet" href="lib/jasmine-2.5.2/jasmine.css">

  <script src="lib/jasmine-2.5.2/jasmine.js"></script>
  <script src="lib/jasmine-2.5.2/jasmine-html.js"></script>
  <script src="lib/jasmine-2.5.2/boot.js"></script>
  <script src="lib/angular.js"></script>
  <script src="lib/angular-mocks.js"></script>

  <script src="code.js"></script>
  <script src="test.js"></script>

</head>

<body>
</body>
</html>

code.js文件 - 连接服务器

'use strict';

angular.module('MyApp.controller',[]).
  controller('MyCtrl1', ['$scope', '$http', '$location', function ($scope, $http, $location) {
  }])

/* Controller */
angular.module("MyApp", ['MyApp.controller']);

function logInSpecificIp(done)
{
   $http({
    method: 'GET',
    url: 'http://10.100.102.102/web_project/index.php/v1/system/generalInfo'
}).error(function (response) {
    console.log(response);
    expect(response).not.toBe(null);
    done();
}).success(function (response) {
    console.log(response);
    expect(response).not.toBe(null);
    expect(response).not.toBeUndefined();
    done();
});
};

test.js - Jamine测试:

describe("Read general info", function () {

beforeEach(module('MyApp'));
beforeEach(inject(function (_$controller_, _$http_, $injector) {
    $http = _$http_;
}));

it("run test 1=============================================", function (done) {
    logInSpecificIp(done);
});
});

0 个答案:

没有答案