为什么我的$ Q推迟在角单元测试不是解决? [英] Why is my $q deferred not resolving in Angular unit test?

查看:260
本文介绍了为什么我的$ Q推迟在角单元测试不是解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我努力让我的第一个的角单元测试工作。我是pretty新来了很多的概念,但因为我已经有了跳重重困难远远得到这个,我张贴了code。我已经看到了帮我解决这一个类似的问题,但没有。

I'm struggling to get my first Angular unit test to work. I'm pretty new to a lot of the concepts, but since I've already had to jump numerous hurdles to get this far, I'm posting the code. I've seen similar questions but none that helped me solve this one.

下面是一个控制器:

.controller('ThumbsCtrl', 
    ['$scope', 'backend', function($scope, backend) {
        backend.getImages()
            .then(function(images) {
                console.log("promise.then() called with "+images);
                $scope.images = images;
            }, function(err) {
               console.log(err);
            });
       }]
)

下面是一个测试:

describe('ThumbsCtrl', function() {
    var controller, rootScope;
    beforeEach(module('myApp.controllers'));
    beforeEach(inject(function($controller, $rootScope) {
        controller = $controller;
        rootScope = $rootScope;
    }));

    it('should contain images', inject(function($q) {
        var scope = rootScope.$new();
        var backend = {
            getImages: function() { 
                console.log("calling mock getImages()");
                var deferred = $q.defer();
                deferred.resolve('shite');
                return deferred.promise;
            }
        };
        var ctrl = controller('ThumbsCtrl', {
            $scope: scope,
            backend: backend
        });

        expect(scope.images).toContain('shite');
    }));
});

由于$ scope.images从未设置的测试失败。

The test fails because $scope.images is never set.

暂且不论评论关于有我的控制器的构造函数中调用异步方法的理智(实际backend.getImages()也返回一个承诺),谁能告诉我,为什么既不在。然后成功或失败的方法()调用曾经被称为?我的理解是通过调用deferred.resolve()作为我的单元测试真的不应该有异步code。但显然我错了,对吧?

Putting aside comments about the sanity of having my controller's constructor call an async method (the actual backend.getImages() also returns a promise), can anyone tell me why neither the success or failure methods in the .then() call are ever called? My understanding is that by calling deferred.resolve() as I am, the unit test really should have no async code. But clearly I'm wrong... Right?

推荐答案

好了,我把它加入到工作:

Well, I got it to work by adding:

scope.$digest();

在决赛前预期()。我会粉笔这件事仍然是一​​个有点$ Q deferreds混淆。我是从使用Parse.com JS SDK,他们的承诺是同步的,当他们已经​​解决了()称为到来。我猜$ Q deferreds异步仍然即使如上解决?

Before the final expect(). I'll chalk this up to still being a bit confused by $q deferreds. I was coming from using the Parse.com JS SDK, whose Promises are synchronous when they've already had resolve() called. I guess $q deferreds remain async even when resolved as above?

作为一个方面说明,断言角度支持和文档以及如何进行单元测试是不是truthy。我交叉引用AngularJS文档,一个的优秀的书(精通网络开发与AngularJS),和样品code距离,我觉得最突出的一个星期到阅读所有这些消息人士透露,我无法写一个简单的单元测试。概念学习的数量很大,而且我也没有用茉莉花(已使用QUnit)之前。我试图用噶所以我可以在同一时间这一步推迟,但哎呀 - 如果我不关心一个单元测试很多,我会跳过它现在给我写愚蠢的应用程序。

As a side note, the assertion that Angular supports and documents well how to unit test is not that truthy. I'm cross-referencing the AngularJS docs, an excellent book (Mastering Web Dev with AngularJS), and sample code from both, and I find it rather salient that a week into reading all these sources, I'm having trouble writing a simple unit test. The number of concepts to learn is LARGE, and I had also not used Jasmine before (have used QUnit). I'm trying to defer using Karma so I can take this one step at a time, but jeez -- if I didn't care a LOT about unit testing, I would have skipped it by now to write my stupid app.

这篇关于为什么我的$ Q推迟在角单元测试不是解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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