2014-04-10 83 views
0

我想用QUnit單元測試angularjs,但得到錯誤消息:$ httpBackend.whenGET不是函數,$ httpBackend.when不是函數。我已經包含角度模擬和角度微風服務(http://www.breezejs.com/documentation/breeze-angular-service),它使用角度q庫來承諾和httpbackend,而不是$ .ajax進行數據傳輸。我仍然無法嘲笑任何對服務器的調用。一些示例代碼:單元測試Angular with Breeze

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

測試(「當控制器然後調用它應該與示波器上的正確的數據來創建」,函數(){ 「使用嚴格」;

// 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"); 

});

這段代碼用來在我不使用微風時正常工作,但現在我已經切換了,我似乎無法讓它工作,文檔很差,如何得到這個工作。任何幫助,將不勝感激。

回答

1

我不能告訴你所有的測試細節。我可以提供一些安慰,它可以工作..和你所期望的幾乎一樣。

下面是從測試/功能/ lookups.spec的「ZZA-節點蒙戈」樣品(it's in github),其中我通過$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 ... 

}); 

的 「查找」 服務(應用程序/服務/ 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消費服務啓動,並通過$http去,一路過關斬將微風角服務剛想網絡邊界通過被截停前, $httpBackend

+0

其中flush $ q();存在?我得到的函數不存在 – johnstaveley

+0

請注意,我在第二個'beforeEach'中定義了它。它僅僅是''rootScope。$ apply()'上的糖而不是「flush $ q」或「flushPromiseQueue」。 – Ward

+0

嗨,注入功能是特定於茉莉花,因爲我說我使用QUnit。 – johnstaveley