Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 使用哪种类型的间谍进行测试_Angularjs_Unit Testing_Jasmine - Fatal编程技术网

Angularjs 使用哪种类型的间谍进行测试

Angularjs 使用哪种类型的间谍进行测试,angularjs,unit-testing,jasmine,Angularjs,Unit Testing,Jasmine,我知道当你使用spyOn时,你可以有不同的形式,比如和.callFake或和callthrough。我不确定我要测试的代码需要哪一个 var lastPage = $cookies.get("ptLastPage"); if (typeof lastPage !== "undefined") { $location.path(lastPage); } else { $location.path('/home'); //TRYING TO

我知道当你使用
spyOn
时,你可以有不同的形式,比如
和.callFake
和callthrough
。我不确定我要测试的代码需要哪一个

  var lastPage = $cookies.get("ptLastPage");
      if (typeof lastPage !== "undefined") {
        $location.path(lastPage);
      } else {
        $location.path('/home'); //TRYING TO TEST THIS ELSE STATEMENT
      }
    }
以下是我的一些测试代码:

describe('Spies on cookie.get', function() {
    beforeEach(inject(function() {
      spyOn(cookies, 'get').and.callFake(function() {
        return undefined;
      });
    }));
    it("should work plz", function() {
      cookies.get();
      expect(location.path()).toBe('/home');
      expect(cookies.get).toHaveBeenCalled();
      expect(cookies.get).toHaveBeenCalledWith();
    });
  });
我尝试了很多不同的方法,但我正在尝试测试
else
语句。因此,我需要制作
cookies.get==未定义的
。
但每次我尝试这样做时,都会出现以下错误:

Expected '' to be '/home'.
cookies.get()
等于
undefined
时,
location.path()。我想我用错了间谍

跟进我的模拟价值观:

beforeEach(inject(
    function(_$location_, _$route_, _$rootScope_, _$cookies_) {
      location = _$location_;
      route = _$route_;
      rootScope = _$rootScope_;
      cookies = _$cookies_;
    }));
职能的后续行动:

angular.module('buildingServicesApp', [
   //data
  .config(function($routeProvider) {
    //stuff
  .run(function($rootScope, $location, $http, $cookies) 

这些函数没有名称,因此如何调用
cookies.get

现在,您正在测试
location.path()
函数是否按设计工作。我想你应该把测试留给AngularJS团队:)。相反,请验证是否正确调用了该函数:

  describe('Spies on cookie.get', function() {
    beforeEach((function() { // removed inject here, since you're not injecting anything
      spyOn(cookies, 'get').and.returnValue(undefined); // As @Thomas noted in the comments
      spyOn(location, 'path');
    }));
    it("should work plz", function() {
      // cookies.get(); replace with call to the function/code which calls cookies.get()
      expect(location.path).toHaveBeenCalledWith('/home');
    });
  });

请注意,您不应该测试您的测试是否模拟了
cookies。get
,您应该测试调用问题中第一位代码的函数是否做了正确的事情。

什么是
cookies
?它是从哪里来的?如果这是模拟
$cookies
,那么它是错误的
$cookies
不被注入,
cookies
也不是
$cookies
。有一个快捷方式:
spyOn(cookies,'get')。和.returnValue(未定义)我添加了一个编辑来帮助澄清我的模拟。不知道你是不是这个意思,但我的代码中确实有这些模拟,只是没有显示出来。嘿,谢谢你的精彩回复,伙计。不幸的是,我调用函数/代码的问题是没有一个?这是我的问题之一。。。我将添加另一个编辑和解释。您可以查看以下问题:。恐怕除了控制器和指令之外,我对角度测试知之甚少。