使用 Jasmine 进行 Angular 单元测试:如何删除或修改 spyOn [英] Angular unit testing with Jasmine: how to remove or modify spyOn

查看:27
本文介绍了使用 Jasmine 进行 Angular 单元测试:如何删除或修改 spyOn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AngularJS v1.2.26

AngularJS v1.2.26

茉莉花 v2.2.0

如何更改或删除 spyOn 的行为?当我尝试覆盖它时,出现以下错误:Error: getUpdate has been spyed

How can I change or remove the behavior of a spyOn? When I try to override it, I get the following error: Error: getUpdate has already been spied upon

var data1 = 'foo';
var data2 = 'bar';

describe("a spec with a spy", function(){

    beforeEach(module('app'));

    var $q;

    beforeEach(inject(function(_updateService_, _$q_){
        updateService = _updateService_;

        //spy the results of the getUpdate()
        $q = _$q_;
        var deferred = $q.defer();
        deferred.resolve( data1 );
        spyOn(updateService, 'getUpdate').and.returnValue(deferred.promise);

    }));

    describe('and here the spy should be different', function() {

        it('returns a different value', function() {

          var deferred = $q.defer();
          deferred.resolve( data2 );
          spyOn(updateService, 'getUpdate'); //ERROR HERE
          updateService.getUpdate.and.returnValue(deferred.promise);

          ...

        });
    });

...

当我删除第二个 spyOn 时,测试不起作用.

When I remove the second spyOn the test doesn't work.

我该怎么做?

推荐答案

覆盖即可

updateService.getUpdate = jasmine.createSpy().and.returnValue(etc)

这篇关于使用 Jasmine 进行 Angular 单元测试:如何删除或修改 spyOn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆