AngularJs 单元测试 - 嘲笑承诺不执行“然后”

2023-11-24

我们正在对控制器进行单元测试。我们已经成功模拟了对 REST 服务层的调用,并验证了它确实是使用给定的数据进行调用的。然而现在我们想在我们的控制器中测试then承诺改变location.path:

控制器:

(function () {

    app.controller('registerController', ['$scope', '$location', '$ourRestWrapper', function ($scope, $location, $ourRestWrapper) {

    $scope.submitReg = function(){
        // test will execute this
        var promise = $ourRestWrapper.post('user/registration', $scope.register);

        promise.then(function(response) {    
                console.log("success!"); // test never hits here           
                $location.path("/");
        },
            function(error) {
                console.log("error!"); // test never hits here
                $location.path("/error");
            }
        );
    };

$ourRestWrapper.post(url,data)只是包裹Restangular.all(url).post(data)..

我们的测试:

(function () {

    describe("controller: registerController", function() {

        var scope, location, restMock, controller, q, deferred;

        beforeEach(module("ourModule"));

        beforeEach(function() {
            restMock = {
                post: function(url, model) {
                    console.log("deferring...");
                    deferred = q.defer();    
                    return deferred.promise;
                }
            };
        });

        // init controller for test
        beforeEach(inject(function($controller, $rootScope, $ourRestWrapper, $location, $q){
            scope = $rootScope.$new();
            location = $location;
            q = $q;

            controller = $controller('registerController', {
                $scope: scope, $location: location, $ourRestWrapper: restMock});
        }));

    it('should call REST layer with registration request', function() {
        scope.register = {data:'test'};

        spyOn(restMock, 'post').andCallThrough();

        scope.submitReg();

        deferred.resolve();

        // successfull
        expect(restMock.post).toHaveBeenCalledWith('user/registration',scope.register);
        expect(restMock.post.calls.length).toEqual(1);
        // fail: Expected '' to be '/'.
        expect(location.path()).toBe('/');
    });

在我们的控制台中,我们看到“deferring...”并且前两个期望成功。为什么它不会调用then块(即设置位置)?


缓存$rootscope当您从注入器获取对象并调用时$rootScope.$apply()之后立马deferred.resolve().

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

AngularJs 单元测试 - 嘲笑承诺不执行“然后” 的相关文章

随机推荐

  • HTML 页面中的抗锯齿字体

    有没有一种好方法可以在网页中创建清晰 清晰的大字体 我需要在我的主页上创建具有不同字体大小和颜色的标签云效果 我已经在 HTML CSS 中设置了它 但在较旧的浏览器或操作系统上默认不支持抗锯齿 它看起来有点 蹩脚 我玩过sIFR 它工作得
  • 如何沿圆移动点?

    我想通过使用圆半径和移动角度来以圆周运动移动精灵 例如 我知道精灵正在以半径 10 的圆内移动 当前位置为 387 38 角度为 28 度 现在我不想将它沿圆周移动 100px p1 x y coordinate known 387 38
  • 升级到 Material UI 4 - withStyles 后出现错误

    从 v3 9 x 升级到 MUI v4 0 2 后 我收到以下错误 您必须将组件传递给 connect 返回的函数 相反收到了 propTypes displayName WithStyles MyComponent options def
  • 使用 DirectX 的桌面捕获不起作用

    由于D3DPOOL SCRATCH处理速度较慢 所以我编写了桌面捕获程序以参考网上的报告 然而 结果却是一片漆黑的画面 这是控制台程序的结果还是有其他原因 include
  • OSX 10.9 上的 Pstack 进程

    在linux上为了查看给定时间的进程堆栈我们一般使用 堆栈pid gt 我想知道 osx 中的相同内容是什么 thanks sudo usr libexec stackshot i u p
  • 从 Java 文本文件中读取特定行

    有没有什么方法可以从文本文件中读取特定行 在 API 或 Apache Commons 中 就像是 String readLine File file int lineNumber 我同意它的实现很简单 但它的效率不是很高 特别是如果文件很
  • 在 lapply/ldply 的列表中使用对象名称

    在试图回答时一个问题早些时候 我遇到了一个看起来应该很简单的问题 但我无法弄清楚 如果我有一个数据框列表 df1 lt data frame a 1 3 x rnorm 3 df2 lt data frame a 1 3 x rnorm 3
  • Span 文本更改上的 MutationObserver 不会触发

    这只是一个简单的例子 而不是实际情况 MutationObserver 仍然没有触发 所以我对其工作原理的假设是错误的 JSFiddle function var editButtonVisibility function console
  • 图像上缩进的透明箭头/三角形

    我想做一个图像上的透明箭头 该三角形应在半透明块中缩进并显示背景图像 期望的输出 barShow background color 000 opacity 0 5 barShow before top 0 left 50 border so
  • 如何在 Markdown 中的文本上应用颜色

    我想使用 Markdown 来存储文本信息 但快速谷歌搜索显示 Markdown 不支持颜色 另外 Stack Overflow 不支持颜色 与 GitHub markdown 的情况相同 有没有允许彩色文本的 Markdown 风格 TL
  • TypeError:只有长度为 1 的数组可以使用 NUMPY 转换为 Python 标量

    coding utf 8 import matplotlib pyplot as plt import numpy as np import math task 2e x np linspace 0 0001 0 1 50 constant
  • .rpt设计时如何使水晶报表页面宽度变大

    我正在寻找一种调整页面宽度大小的方法 Crystal Reports 使我可以将字段放置在 rpt 文件中 到目前为止 我发现如果右键单击报告 选择 设计 打印机设置 我可以选择要打印报告的纸张尺寸 如果我选择 方向 横向 我可以增加页面的
  • 使用 prism.js 突出显示 html

    我似乎无法使用 prism js 突出显示 html 因为它删除了仅打印文本的标记 pre 标签内的以下代码仅显示为文本 我将 代码 标签的类设置为 语言标记 table class data table tr td Title td td
  • 将 foreach 与 LINQ to Entities 结合使用时是否需要 ToList

    我有一个关于 LINQ 如何与foreach通过实体框架中的可查询实体 做哪一个更好 更快 foreach var thing in myentities GetThemAll or foreach var thing in myentit
  • 重定向根 URL - IIS 7

    我的网站上安装了一些论坛http ironnoob com forums 我的网站位于 c IronNoob 我的论坛安装在 c IronNoob forums 我希望 ironnoob com 和 www ironnoob com 都进行
  • 如何在docker容器中伪造cpu架构?

    当我创建 32 位 CentOS 5 docker 映像时 我希望 cpu 架构报告为 i386 i686 安装程序在此容器检查架构中进行测试并安装 64 位二进制文 件而不是 32 位 我设置了 yum 变量并创建了 uname 包装器
  • 批处理文件中删除特定扩展名的文件

    我想递归删除批处理文件中具有特定扩展名的所有文件 我知道以下命令 del s ext 但是 这在 Windows 上也会删除具有其他扩展名的文件 例如 ext1 或 ext2 其原因似乎是此类文件的 8 3 文件名以 ext 结尾 因此扩展
  • 实体框架 EF.Functions.Like 与 string.Contains

    我正在阅读实体框架核心2 0的公告https blogs msdn microsoft com dotnet 2017 08 14 announcing entity framework core 2 0 它说他们添加了新的 Sql 函数
  • 下拉 onchange 调用 PHP 函数

    我尝试使用下面的代码从下拉菜单中调用 PHP 函数 有没有一种干净的方法可以做到这一点 code section section
  • AngularJs 单元测试 - 嘲笑承诺不执行“然后”

    我们正在对控制器进行单元测试 我们已经成功模拟了对 REST 服务层的调用 并验证了它确实是使用给定的数据进行调用的 然而现在我们想在我们的控制器中测试then承诺改变location path 控制器 function app contr