【问题标题】:Test a Form submission with jasmine and backbone.使用 jasmine 和骨干测试表单提交。
【发布时间】:2012-08-10 19:25:12
【问题描述】:

我想使用 jasmine 测试表单提交。
该表单在主干视图中定义,如下所示 (1)。
我实现了以下测试(2),但我不确定它是如何有效的。
例如,如果 textarea 为空,则应调用 onError 函数。
在这种情况下,使用 jasmine 测试表单提交的最佳方法有什么想法吗?


(1)

var MyView = Backbone.View.extend({

    events: {
        'focus [data-tid="message"]' : 'focusForm',
        'blur [data-tid="message"]' : 'blurForm',
        'submit   form' : 'submitForm'
    },

    focusedClass: 'is-form-focused',

    focusForm: function () {
        this.$el.find('form').addClass(this.focusedClass);
    },

    blurForm: function () {
        this.$el.find('form').removeClass(this.focusedClass);
    },

    submitForm: function (event) {
        event.preventDefault();

        var formElement =  event.currentTarget,
            message = this.$el.find('.message');

        if (formElement.checkValidity && !formElement.checkValidity()) {
            this.onError();
        } else {
            // makes a POST ajax call
            backendController.submitFeedback(message.val()).done(this.onSuccess).fail(this.onError);

        }
    },

    onError: function () {
        this.$el.find('.message').focus();
    },

    onSuccess: function () {
        this.$el.find('.message').val('');
        this.$el.find('form').removeClass(this.focusedClass);
    }
});

(2)

describe('When Submit button handler fired', function () {
    beforeEach(function () {
        this.popupSpy = sinon.spy();
        this.view.render();
        this.view.$el.find('form').on('submit', this.popupSpy);
        this.view.$el.find('form').trigger('submit');
    });
    it('submitForm should be called', function () {
        expect(this.popupSpy.callCount).toBe(1);
    });
});

【问题讨论】:

    标签: javascript backbone.js jasmine


    【解决方案1】:

    在您的示例中,您正在测试自己的测试。

    我宁愿提出这样的建议:

    // code simplified and no tested
    describe("When Submit button handler fired", function () {
      it("submitForm should be called", function () {
        this.view.render();
        spyOn( this.view, "submitForm" );
        this.view.$el.find("form").submit();
        expect( this.view.submitForm ).toHaveBeenCalled();
      });
    });
    

    更新

    可能我上面的代码不会工作,因为就像在spying over Router methods 中一样,处理程序是在初始化时设置的,所以不会再调用spy

    您应该在类级别和实例化视图之前进行监视:

    // code simplified and no tested
    describe("When Submit button handler fired", function () {
      it("submitForm should be called", function () {
        spyOn( MyView.prototype, "submitForm" );
    
        this.view = new MyView();
        this.view.render();
    
        this.view.$el.find("form").submit();
    
        expect( MyView.prototype.submitForm ).toHaveBeenCalled();
      });
    });
    

    【讨论】:

    • +1 感谢您的回复。实际上我收到以下错误Expected spy submitForm to have been called 你确定你写的代码吗?谢谢
    • // 代码简化且未经过测试 ;).. 我只是提供一个起点,可能你应该清理一下。
    • 我可以在工作示例中工作,但你应该给我一个游乐场,你可以 fork this jsFiddle
    • @LorraineBernard 检查我的更新。如果它仍然无法正常工作,请查看我上面的评论。
    • 这一次它似乎工作了,但是有一个大问题:我在进行测试的页面浏览器上出现了一个无限循环:(
    猜你喜欢
    • 2012-08-10
    • 2013-03-17
    • 2013-06-25
    • 2019-03-13
    • 1970-01-01
    • 2016-09-16
    • 1970-01-01
    • 1970-01-01
    • 2015-01-22
    相关资源
    最近更新 更多