[英]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.