如何使用 Jasmine 对链式方法进行单元测试

How to unit test a chained method using Jasmine(如何使用 Jasmine 对链式方法进行单元测试)
本文介绍了如何使用 Jasmine 对链式方法进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我在对以下方法进行单元测试时遇到问题:

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

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

我编写了以下单元测试,但当前失败并出现此错误(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);
    });

如果我将函数更改为以下,则测试通过:

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

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

当我使用方法链接时,如何对这个方法进行单元测试?我需要以不同的方式设置我的 $locationMock 吗?对于我的生活,我无法弄清楚这一点.

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.

推荐答案

那是因为你的 mock 没有返回 location 对象以便能够链接.使用 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

或者只是创建一个间谍对象(更少的代码):

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

这篇关于如何使用 Jasmine 对链式方法进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

jasmine.createSpyObj with properties(jasmine.createSpyObj 与属性)
What#39;s a good way to reuse test code using Jasmine?(使用 Jasmine 重用测试代码的好方法是什么?)
Does Jasmine#39;s toThrow matcher require the argument to be wrapped in an anonymous function?(Jasmine 的 toThrow 匹配器是否需要将参数包装在匿名函数中?)
How to mock window.location.href with Jest + Vuejs?(如何用 Jest + Vuejs 模拟 window.location.href?)
Jasmine - Spying on a method call within a constructor(Jasmine - 在构造函数中监视方法调用)
Getting requirejs to work with Jasmine(让 requirejs 与 Jasmine 一起工作)