久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Jasmine angularjs - 監視初始化控制器時調用的方法

Jasmine angularjs - spying on a method that is called when controller is initialized(Jasmine angularjs - 監視初始化控制器時調用的方法)
本文介紹了Jasmine angularjs - 監視初始化控制器時調用的方法的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我目前正在使用 Jasmine 和 Karma(Testacular) 和 Web Storm 來編寫單元測試.我無法監視初始化控制器時立即調用的方法.是否可以窺探控制器初始化時調用的方法?

I am currently using Jasmine with Karma(Testacular) and Web Storm to write unit test. I am having trouble spying on a method that gets called immediately when the controller is initialized. Is it possible to spy on a method that is called when the controller is initialized?

我的控制器代碼,我試圖監視的方法是 getServicesNodeList().

My controller code, the method I am attempting to spy on is getServicesNodeList().

myApp.controller('TreeViewController', function ($scope, $rootScope ,$document, DataServices) {
    $scope.treeCollection  =  DataServices.getServicesNodeList();
    $rootScope.viewportHeight = ($document.height() - 100) + 'px';
});

這是測試規范:

describe("DataServices Controllers - ", function () {

beforeEach(angular.mock.module('myApp'));
describe("DataServicesTreeview Controller - ", function () {


    beforeEach(inject(function ($controller, $rootScope, $document, $httpBackend, DataServices) {
        scope = $rootScope.$new(),
        doc = $document,
        rootScope = $rootScope;
        dataServices = DataServices;

        $httpBackend.when('GET', '/scripts/internal/servicedata/services.json').respond(...);

        var controller = $controller('TreeViewController', {$scope: scope, $rootScope: rootScope, $document: doc, DataServices: dataServices });

        $httpBackend.flush();
    }));

    afterEach(inject(function($httpBackend){
        $httpBackend.verifyNoOutstandingExpectation();
        $httpBackend.verifyNoOutstandingRequest();
    }));

    it('should ensure DataServices.getServicesNodeList() was called', inject(function ($httpBackend, DataServices) {
        spyOn(DataServices, "getServicesNodeList").andCallThrough();

        $httpBackend.flush();
        expect(DataServices.getServicesNodeList).toHaveBeenCalled();
    }));


});
});

測試失敗,說明該方法沒有被調用.我知道我應該模擬 DataServices 并將其傳遞給測試控制器.但是,無論它是否是模擬方法,在監視該方法時,我似乎仍然會遇到同樣的問題.任何人有任何想法或可以指出我處理此問題的正確方法的資源?

The test is failing saying that the method has not been called. I know that I should mock the DataServices and pass that into the test controller. But it seems like I would still have the same problem when spying on that method whether it is a mock or not. Anyone have any ideas or could point me to resources on the correct way to handle this?

推薦答案

在編寫單元測試時,應該隔離每一段代碼.在這種情況下,您需要隔離您的服務并單獨對其進行測試.創建服務的模擬并將其傳遞給您的控制器.

When writing unit tests, you should isolate each piece of code. In this case, you need to isolate your service and test it separately. Create a mock of the service and pass it to your controller.

var mockDataServices = {
    getServicesNodeList: function () {
        return <insert your sample data here > ;
    }
};

beforeEach(inject(function ($controller, $rootScope, $document) {
    scope = $rootScope.$new(),
    doc = $document,
    rootScope = $rootScope;

    var controller = $controller('TreeViewController', {
        $scope: scope,
        $rootScope: rootScope,
        $document: doc,
        DataServices: mockDataServices
    });
}));

如果發出 $http 請求的是您的服務,您可以從單元控制器測試中刪除該部分.編寫另一個單元測試來測試服務在初始化時是否進行了正確的 http 調用.

If it is your service that is making the $http request, you can remove that portion from your unit controller test. Write another unit test that tests that the service is making the correct http calls when it is initialized.

這篇關于Jasmine angularjs - 監視初始化控制器時調用的方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認為文檔“準備好之前,如何讓我的 jasmine 測試裝置加載?) - IT屋-程序員軟件開發技術
What do jasmine runs and waitsFor actually do?(jasmine 運行和等待實際上是做什么的?)
How to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模擬文件來更改 lt;input type=filegt; 的事件用于單元測試)
How to unit test a chained method using Jasmine(如何使用 Jasmine 對鏈式方法進行單元測試)
How do I inject $rootScope into an AngularJS unit test?(如何將 $rootScope 注入 AngularJS 單元測試?)
Jasmine - How to spy on a function call within a function?(Jasmine - 如何監視函數中的函數調用?)
主站蜘蛛池模板: 亚洲精品一区二区久 | 欧美精三区欧美精三区 | 久久久成人免费一区二区 | 99久久免费观看 | 青青草久久 | 神马久久久久久久久久 | 亚洲成人中文字幕 | 国产一区二区高清在线 | 欧美日韩亚洲视频 | 国产激情一区二区三区 | www.久草.com | 国产美女黄色片 | 成人国产精品一级毛片视频毛片 | 欧美精品一区在线发布 | 中文字幕在线免费 | 成人免费网站视频 | 中文字幕欧美一区二区 | 精品久久1| 99精品国产一区二区三区 | 国产精品成人69xxx免费视频 | 中文字幕中文字幕 | 伊人精品在线 | 日本视频在线 | 久久三区| 亚洲日本欧美日韩高观看 | 日韩一区二区久久 | 亚洲成人天堂 | 狠狠综合久久av一区二区小说 | 久久里面有精品 | 久久精品国产99国产精品 | 国产一区二区三区 | 亚洲国产精品视频一区 | 亚洲逼院 | 黄色国产| 久久精品成人热国产成 | 久久精品亚洲成在人线av网址 | 天天干天天操天天看 | 日韩有码在线观看 | 欧美一区免费 | 亚洲国产欧美一区二区三区久久 | 成人精品一区亚洲午夜久久久 |