Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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
Javascript AngularJs单元测试-模拟承诺不执行“;然后";_Javascript_Unit Testing_Angularjs_Mocking_Promise - Fatal编程技术网

Javascript AngularJs单元测试-模拟承诺不执行“;然后";

Javascript AngularJs单元测试-模拟承诺不执行“;然后";,javascript,unit-testing,angularjs,mocking,promise,Javascript,Unit Testing,Angularjs,Mocking,Promise,我们正在对控制器进行单元测试。我们已经成功地模拟了对REST服务层的调用,并验证了它确实是使用给定数据调用的。然而,现在我们想测试一下,在我们的控制器中,的执行,然后承诺会改变位置。路径: 控制器: (function () { app.controller('registerController', ['$scope', '$location', '$ourRestWrapper', function ($scope, $location, $ourRestWrapper) {

我们正在对控制器进行单元测试。我们已经成功地模拟了对REST服务层的调用,并验证了它确实是使用给定数据调用的。然而,现在我们想测试一下,在我们的控制器中,
的执行,然后
承诺会改变
位置。路径

控制器:

(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,数据)
只需包装
restanglar.all(url).post(数据)

我们的测试:

(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('/');
    });

在我们的控制台中,我们看到“推迟…”和前两个期望成功。为什么它不调用
然后
块(即设置位置)?

当您从注入器获取
$rootscope
对象并在
延迟后立即调用
$rootscope.$apply()
。resolve()
哇!有人能分享一下为什么这能解决这个问题吗?我已经为此奋斗了一整天。我要感谢你的回答。“不执行
then
”这个问题让我在凌晨睡不着觉。做了一次快速的谷歌搜索,在这里找到了答案:>简而言之,在单元测试中,
$digest
循环没有被调用,因此调用
$rootScope.$apply()
会强制运行摘要循环