繁体   English   中英

使用Breeze进行Angular单元测试

[英]Unit testing Angular with Breeze

我正在尝试使用QUnit对angularjs进行单元测试,但会收到错误消息:$ httpBackend.whenGET不是函数,$ httpBackend.when不是函数。 我已经包含了角度模拟和角度微风服务( http://www.breezejs.com/documentation/breeze-angular-service ),该服务使用angular q库进行promise和httpbackend而不是$ .ajax进行数据传输。 我仍然无法模拟对服务器的任何调用。 一些示例代码:

var $httpBackend,
    injector;
    var SPAModule = angular.module("spa");
    injector = angular.injector(['ng', 'spa']);
    $httpBackend = injector.get("$httpBackend");
    SPAModule.config(function ($provide) {
        $provide.decorator('$httpBackend', angular.mock.e2e.$httpBackendDecorator);
    });

test(“当控制器被称为THEN时,应该在示波器上使用正确的数据来创建它”,function(){'use strict';

// Given
$httpBackend.whenGET("/Breeze/Data/Jobs").respond({ data: jobData });
$httpBackend.whenGET("/Breeze/Data/Metadata").respond({});
var routeParams = { id: "b" },

// When
    controller = injector.get('$controller')(toriga.propertyController, {
        $scope: theScope,
        $window: windowMock,
        $location: locationMock,
        $routeParams: routeParams
    }),
    $rootScope = injector.get('$rootScope');
$httpBackend.flush();
$rootScope.$apply(); // forces results of promise to be executed

// Then
notEqual(controller, null, 'controller was created properly');
strictEqual(theScope.pageTitle, "Property", "pageTitle was set on the scope");
notEqual(theScope.job, null, "Job set on the scope");
ok(toastrMock.warning.notCalled, "No warning messages were displayed");
ok(toastrMock.error.notCalled, "No error messages were displayed");

});

当我不使用微风时,该代码过去可以正常工作,但是现在我切换了,似乎无法正常工作,并且文档上关于如何使它正常工作的信息也很贫乏。 任何帮助,将不胜感激。

我无法告诉您测试的所有详细信息。 我可以提供一些安慰,以确保它确实可以正常工作..和您期望的差不多。

这是“ Zza-Node-Mongo”示例( 在github中 )中test / specs / lookups.spec的摘录,其中我通过$httpBackend模拟重播服务器对Breeze客户端请求的响应(子集)用于“查找”参考实体。

我使用的是Jasmine而不是QUnit,但希望您能理解。

// simplified for presentation here but materially sufficient
describe("when lookups service receives valid lookups data", function () {

    var $httpBackend, flush$q, lookups
    var lookupsUrlRe = /breeze\/zza\/Lookups\?/; // RegEx of the lookups endpoint

    beforeEach(module('app')); 

    beforeEach(inject(function(_$httpBackend_, $rootScope, _lookups_) {
        $httpBackend = _$httpBackend_;
        flush$q = function() { $rootScope.$apply(); };
        lookups = _lookups_;
    }));

    beforeEach(function () {
        $httpBackend.expectGET(lookupsUrlRe).respond(validLookupsResponse.data);
        lookups.ready(); // THIS TRIGGERS CALL TO FETCHLOOKUPS
        $httpBackend.flush();
    });

    it("doesn't bomb", function () {
        expect(true).toBe(true);
    });

    it("'ready()' invokes success callback", function () {
       var success = jasmine.createSpy('success');
        lookups.ready(success);
        flush$q();  // NOTE NEED TO FLUSH $Q IN THIS TEST
        expect(success).toHaveBeenCalled();
    })

    it("has OrderStatus.Pending", function () {
        expect(lookups.OrderStatus && lookups.OrderStatus.Pending).toBeDefined();
    });

    ... more tests ...

});

“查找”服务( app / services / lookups.js )调用微风从服务器获取查找数据。

function fetchLookups() {
   return breeze.EntityQuery.from('Lookups')
       .using(manager).execute()
       .then(function () {
           logger.info("Lookups loaded from server.");
           extendService(manager)
       })
       .catch(function (error) {
           error = util.filterHttpError(error);
           logger.error(error.message, "lookups initialization failed");
           throw error; // so downstream fail handlers hear it too
       });
}

就像您可能想象的那样,这是一个非常深入的集成测试,首先从ViewModel消耗的服务开始,一直到$httpBackend拦截之前,一直通过$http穿过Breeze Angular Service一直到网络边界。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM