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

如何使用 Jasmine 監視匿名函數

How to spy on anonymous function using Jasmine(如何使用 Jasmine 監視匿名函數)
本文介紹了如何使用 Jasmine 監視匿名函數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在使用 Jasmine 測試我的 Angular 應用程序并希望監視一個匿名函數.使用 angular-notify 服務 https://github.com/cgross/angular-notify,我想要知道通知函數是否被調用.

I'm using Jasmine to test my angular application and want to spy on an anonymous function. Using angular-notify service https://github.com/cgross/angular-notify, I want to know whether notify function have been called or not.

這是我的控制器:

angular.module('module').controller('MyCtrl', function($scope, MyService, notify) {

  $scope.isValid = function(obj) {
    if (!MyService.isNameValid(obj.name)) {
      notify({ message:'Name not valid', classes: ['alert'] });
      return false;
    }
  }
});

這是我的測試:

'use strict';

describe('Test MyCtrl', function () {
  var scope, $location, createController, controller, notify;

  beforeEach(module('module'));

  beforeEach(inject(function ($rootScope, $controller, _$location_, _notify_) {
    $location = _$location_;
    scope = $rootScope.$new();
    notify = _notify_;

    notify = jasmine.createSpy('spy').andReturn('test');

    createController = function() {
      return $controller('MyCtrl', {
        '$scope': scope
      });
    };
  }));

  it('should call notify', function() {
    spyOn(notify);
    controller = createController();
    scope.isValid('name');
    expect(notify).toHaveBeenCalled();
  });
});

一個明顯的回報:

Error: No method name supplied on 'spyOn(notify)'

因為它應該是類似 spyOn(notify, 'method') 的東西,但是由于它是一個匿名函數,所以它沒有任何方法.

Because it should be something like spyOn(notify, 'method'), but as it's an anonymous function, it doesn't have any method.

感謝您的幫助.

推薦答案

Daniel Smink 的答案是正確的,但請注意 Jasmine 2.0 的語法已更改.

Daniel Smink's answer is correct, but note that the syntax has changed for Jasmine 2.0.

notify = jasmine.createSpy().and.callFake(function() {
  return false;
});

如果您只需要一個簡單的實現,我還發現直接返回響應很有用

I also found it useful to just directly return a response if you only need a simple implementation

notify = jasmine.createSpy().and.returnValue(false);

這篇關于如何使用 Jasmine 監視匿名函數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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| 国产精品福利在线 | 成人在线中文字幕 | 九九久久精品视频 | 国产成人亚洲精品自产在线 | 久久久久久国产精品免费免费 | 美女在线视频一区二区三区 | av一级久久| 精品国产乱码久久久久久a丨 | 精品国产一区二区国模嫣然 | 国产一区二区三区在线 | 亚洲一区二区三区在线 | 日本久久久久久 | 欧美日韩电影免费观看 | 久久新视频 | 日本在线视频一区二区 | 日韩成人免费av | 欧美一区二区三区久久精品视 | av电影一区 | 久草资源在线视频 | 国产一区二区三区视频免费观看 | 91精品国产综合久久精品图片 | 91麻豆精品国产91久久久久久久久 | 成人综合在线视频 | 精品无码久久久久久久动漫 | 99这里只有精品视频 | 91九色视频 | 色综合天天天天做夜夜夜夜做 | www天天操| 一级美国黄色片 | 精品国产免费一区二区三区演员表 | 最新国产精品精品视频 | 成人在线视 | 日本激情一区二区 | 免费国产视频在线观看 | 日韩中文一区二区三区 | 久久高清国产 | 在线观看亚洲 | 中文字幕一区二区三区四区 | 精品久久久网站 | 国产精品视频综合 |