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

如何使用 Jasmine 對(duì)鏈?zhǔn)椒椒ㄟM(jìn)行單元測(cè)試

How to unit test a chained method using Jasmine(如何使用 Jasmine 對(duì)鏈?zhǔn)椒椒ㄟM(jìn)行單元測(cè)試)
本文介紹了如何使用 Jasmine 對(duì)鏈?zhǔn)椒椒ㄟM(jìn)行單元測(cè)試的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時(shí)送ChatGPT賬號(hào)..

我在對(duì)以下方法進(jìn)行單元測(cè)試時(shí)遇到問題:

I'm having a problem unit testing the following method:

 $scope.changeLocation = function (url) {
        $location.path(url).search({ ref: "outline" });
    };

我編寫了以下單元測(cè)試,但當(dāng)前失敗并出現(xiàn)此錯(cuò)誤(TypeError: Cannot read property 'search' of undefined):

I've written the following unit test that currently fails with this error (TypeError: Cannot read property 'search' of undefined):

var $locationMock = { path: function () { }, search: function () { } };

it('changeLocation should update location correctly', function () {
        $controllerConstructor('CourseOutlineCtrl', { $scope: $scope, $location: $locationMock });

        var url = "/url/";
        spyOn($locationMock, "path");
        spyOn($locationMock, "search");

        $scope.changeLocation(url);

        expect($locationMock.search).toHaveBeenCalledWith({ ref: "outline" });
        expect($locationMock.path).toHaveBeenCalledWith(url);
    });

如果我將函數(shù)更改為以下,則測(cè)試通過:

If I change my function to the following, the test passes:

$scope.changeLocation = function (url) {
        $location.path(url);
        $location.search({ ref: "outline" });
    };

當(dāng)我使用方法鏈接時(shí),如何對(duì)這個(gè)方法進(jìn)行單元測(cè)試?我需要以不同的方式設(shè)置我的 $locationMock 嗎?對(duì)于我的生活,我無法弄清楚這一點(diǎn).

How do I unit test this method when I'm using method chaining? Do I need to setup my $locationMock differently? For the life of me I cannot figure this out.

推薦答案

那是因?yàn)槟愕?mock 沒有返回 location 對(duì)象以便能夠鏈接.使用 Jasmine 2.0,您可以將模擬更改為:

That is because your mock does not return location object to be able to chain through. Using Jasmine 2.0 you can change your mock to:

var $locationMock = { path: function () { return $locationMock; }, 
                      search: function () { return $locationMock; } };

spyOn($locationMock, "path").and.callThrough();
spyOn($locationMock, "search").and.callThrough(); //if you are chaining from search

或添加:

spyOn($locationMock, "path").and.returnValue($locationMock);
spyOn($locationMock, "search").and.returnValue($locationMock); //if you are chaining from search

或者只是創(chuàng)建一個(gè)間諜對(duì)象(更少的代碼):

Or just create a spy object (less code):

var $locationMock = jasmine.createSpyObj('locationMock', ['path', 'search']);

$locationMock.path.and.returnValue($locationMock);
$locationMock.search.and.returnValue($locationMock); //if you are chaining from search

這篇關(guān)于如何使用 Jasmine 對(duì)鏈?zhǔn)椒椒ㄟM(jìn)行單元測(cè)試的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認(rèn)為文檔“準(zhǔn)備好之前,如何讓我的 jasmine 測(cè)試裝置加載?) - IT屋-程序員軟件開發(fā)技術(shù)
What do jasmine runs and waitsFor actually do?(jasmine 運(yùn)行和等待實(shí)際上是做什么的?)
How to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模擬文件來更改 lt;input type=filegt; 的事件用于單元測(cè)試)
How do I inject $rootScope into an AngularJS unit test?(如何將 $rootScope 注入 AngularJS 單元測(cè)試?)
Jasmine - How to spy on a function call within a function?(Jasmine - 如何監(jiān)視函數(shù)中的函數(shù)調(diào)用?)
jasmine test fails with undefined is not a function(evaluating $browser.$$checkUrlChange())(茉莉花測(cè)試失敗,未定義不是函數(shù)(評(píng)估 $browser.$$checkUrlChange()))
主站蜘蛛池模板: 天天影视综合 | 中文字幕在线看 | 一区二区三区四区电影 | 天天操天天干天天曰 | 国产精品成人一区二区 | aaa级片| 日韩电影在线一区 | 91精品国产综合久久久久久 | 久久99深爱久久99精品 | 亚洲精彩视频在线观看 | 亚洲成人精品 | 成人在线影视 | 午夜男人免费视频 | 麻豆久久精品 | 亚洲欧美日韩中文在线 | 久久99精品久久久 | 精品国产乱码久久久久久蜜臀 | 午夜丰满寂寞少妇精品 | 国产一区久久精品 | 99综合| 国产欧美一区二区三区国产幕精品 | 成人在线精品 | 国产一区二区三区视频 | 国产伦精品一区二区三毛 | 国产福利久久 | 久久婷婷色 | 亚洲精品日韩一区二区电影 | 国产精品美女久久久久aⅴ国产馆 | 国产成都精品91一区二区三 | 久久久激情 | 超碰97人人人人人蜜桃 | 国产一区二区三区色淫影院 | 久久精品99国产精品 | 国产精品69毛片高清亚洲 | 亚洲欧洲综合av | 99视频入口 | 成人av免费在线观看 | 手机在线一区二区三区 | 欧美综合一区二区 | 国产精品精品视频一区二区三区 | 成人免费看黄网站在线观看 |