2012-04-19 25 views
5

Tôi đã viết mã bên dưới, hãy thử kiểm tra xem hộp thoại jquery có được phép và hiển thị không.Tôi có thể hiển thị hộp thoại jquery kiểm tra đơn vị bằng cách nào?

var jqueryMock = sinon.mock(jQuery); 
var dialogExpectation = jqueryMock.expects("dialog"); 
dialogExpectation.once(); 

//call my function, in which create a jquery dialog. 

equals(dialogExpectation.verify(), true, "Dialog is displayed"); 
jqueryMock.restore(); 

Tuy nhiên, nó cho thấy tôi những lỗi: Chết trên thử nghiệm # 1: Cố gắng để quấn thoại tài sản không xác định như chức năng - { "message": "Đã cố gắng để quấn thoại tài sản không xác định như chức năng", "tên" : "TypeError"}

mã jquery rất đơn giản:

displayMessage: function (message, title, hashId) { 

//some logic to build the message, title and hashId. 

$(messageDiv).dialog({ 
      height: 240, 
      width: 375, 
      modal: true, 
      title: title, 
      resizable: false, 
      buttons: [{ 
       text: localizedErrorMessages['OkText'], 
       click: function() { 
        $(this).dialog("close"); 
       } 
      }]    
     }); // end of dialog    
    } // end of displayMessage 

Bất cứ ai cũng biết làm thế nào để thử hộp thoại jquery và viết bài kiểm tra đơn vị trong tình huống này?

+0

Khung kiểm tra này là gì? – streetlight

Trả lời

3

Bạn cần phải thử jQuery.fn như thế này:

var jQueryMock = sinon.mock(jQuery.fn); 
0

Tôi đã tạo một jsFiddle để chứng minh câu trả lời làm việc.

function displayMessage(message, title, hashId) { 

    $("#message").dialog(); 
} 

test("dialog was called", function() { 

    var jQueryMock = sinon.mock($.fn); // jQuery.fn and $.fn are interchangeable 
    var dialogExpectation = jQueryMock.expects("dialog"); 
    dialogExpectation.once(); 

    //call my function, in which create a jquery dialog. 
    displayMessage("new message", "title", 1); 

    equal(dialogExpectation.verify(), true, "Dialog was not displayed"); 
    jQueryMock.restore(); 
}); 

// This demonstrates a failing test - since the method actuall calls "dialog". 
// It also demonstrates a more compact approach to creating the mock 
test("toggle was called", function() { 

    var mock = sinon.mock(jQuery.fn).expects("toggle").once(); 
    displayMessage("new message", "title", 1); 

    equal(mock.verify(), true, "Toggle was never called"); 
}); 
Các vấn đề liên quan