toMatch Not Working

时间:2014-04-28 15:32:30

标签: javascript jquery testing

有谁知道为什么这不通过?

function correctColorDisplay(message, player_turn, selector) {
  if ((message > 0) && (player_turn != 0)) {
    return $(selector).append("<li>" + message + " " + "color(s) are present but not in the correct position in Round " + player_turn + ".</li>");
  }
}

茉莉花:

describe('#correctColorDisplay', function(){
  it('returns a message to the user displaying if a correct color (not positions) was chosen', function(){
    var message = 2
    var playerTurn = 2
    var selector = $('<li></li>')
    correctColorDisplay(message,playerTurn, selector)
    expect(selector).toMatch("<li>" + message + " " + "color(s) are present but not in the correct position in Round " + playerTurn + ".</li>")
  });
});

我不断得到的错误是这个巨大的消息:预期{0:HTMLNode,长度:1,jquery:'1.11.0',构造函数:Function,selector:'',toArray:Function,get:Function,pushStack:功能,每个等等(它会持续更长时间)

1 个答案:

答案 0 :(得分:1)

您正在尝试将新创建的HTMLNode与正则表达式匹配(在本例中基本上只是一个字符串)。

Jasmine的toMatch函数用于正则表达式。

我对Jasmine并不完全熟悉,但我猜测你正在寻找类似的东西:

describe('#correctColorDisplay', function(){
    it('returns a message to the user displaying if a correct color (not positions) was chosen', function() {
        var message = 2;
        var playerTurn = 2;
        var selector = $('<li></li>');
        selector = correctColorDisplay(message, playerTurn, selector);
        expect(selector).toEqual( $("<li><li>" + message + " " + "color(s) are present but not in the correct position in Round " + playerTurn + ".</li></li>") );
    });
});

如果这不起作用,我建议您查看jasmine-jquery