单元测试$模式茉莉 [英] Unit testing $modal with Jasmine

查看:215
本文介绍了单元测试$模式茉莉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器的角度应用程序,它调用函数时显示一个角绳模式窗口。其正确运行在Chrome,但我很茫然得到一个有效的单元测试工作。

I have an Angular app with a controller which displays an Angular-Strap modal window during a function call. It functions correctly in Chrome, but I am at a loss getting a valid unit test working.

应用模块和FooController的:

var app = angular.module("app", ["mgcrea.ngStrap"]);

app.controller("FooController", function($scope, $modal) {
    var fooModal = $modal({
        title: 'Foo',
        content:'Bar',
        show: false,
        html: true,
        backdrop: 'static',
        placement: 'center'});

    angular.extend($scope, {
        makeItFoo: function() {
            fooModal.show();
        }
    });
});

控制器规格:

describe('FooController', function () {
    var scope, controller, modal;

    beforeEach(module('app', function ($provide) {
        // Stub out $modal service
        $provide.value('$modal', function () {
            return {
                hide: function () { },
                show: function () { }
            };
        });
    }));

    beforeEach(inject(function ($rootScope, $controller, $injector) {
        //set up a new scope and the controller for the test
        scope = $rootScope.$new();
        controller = $controller('FooController', {$scope: scope});
        modal = $injector.get('$modal');
    }));

    it('should show the modal', function () {
        var modalSpy = spyOn(modal(), 'show');

        scope.makeItFoo();

        expect(modalSpy).toHaveBeenCalled();
    });
});

这里有一个小提琴为好。

我希望我的呼吁 makeItFoo()显示模式,但茉莉失败,出现错误的测试来一直呼吁的预期间谍秀。我也尝试设置显示模态的属性真正,而不是调用秀()分别,我尝试了捻熄$模式服务,并直接将其注入控制器的其它变体,但它具有相同的错误结束。

I expect my call to makeItFoo() to display the modal, but Jasmine fails the test with the error Expected spy show to have been called. I've also tried setting the show property of the modal to true and not calling show() separately, and I've tried other variants of stubbing the $modal service and injecting it directly into the controller, but it ends up with the same error.

我使用AngularJS 1.2.14,角带2.0.0和1.3.1茉莉花

I'm using AngularJS 1.2.14, Angular-Strap 2.0.0, and Jasmine 1.3.1.

推荐答案

而不是做这些的。与展会创建 $模式模拟对象和隐藏方法和设备上设置您的期望。

Instead of doing these. Create a mock object for $modal with show and hide methods and set your expectations on them.

describe('FooController', function () {
    var scope, controller, modal;

    beforeEach(module('app'));

    beforeEach(inject(function ($rootScope, $controller) {
        //set up a new scope and the controller for the test
        scope = $rootScope.$new();
        //Create spy object
        modal = jasmine.createSpyObj('modal', ['show', 'hide']);
        //provide modal as dependency to the controller.
        controller = $controller('FooController', {$scope: scope, $modal:modal});

    }));

    it('should show the modal', function () {

        scope.makeItFoo();

        expect(modal.show).toHaveBeenCalled();
    });
});

这篇关于单元测试$模式茉莉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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