2014-03-26 22 views
2

我正在研究一款結合了Ember和jquery-terminal的應用程序,以幫助用戶學習。我對前面的長問題表示歉意!jQuery終端,Ember,QUnit - 自動測試問題

我喜歡以TDD風格工作,但我一直在努力工作。

我想要做的是將用戶輸入模擬成jquery-terminal並聲明在終端中顯示正確的響應。我已經挖掘了源代碼,也許我錯過了一些東西,但我似乎無法找到添加用戶輸入的地方。終端最終輸出的: -

<div id="terminal" class="terminal"> 
    <div class="terminal-output"></div> 
    <div class="cmd" style="width: 100%;"> 
    <span class="prompt">TryRuby:&gt;&nbsp;</span> 
    <span></span> 
    <span class="cursor blink">&nbsp;</span> 
    <span></span> 
    <textarea class="clipboard"></textarea> 
    </div> 
</div> 

由用戶輸入的文本呈現在第一個未命名跨度,但這裏插入文本並不能使它提供給jquery-terminal上提交,我得到了同樣的答覆,如果它是空白的。

我想模擬是這(僞)

test('user enters help and submits', function() { 
    var input = $('#terminal') // find the right node/place to bind is my problem 
    input.text = 'help' 
    keyEvent(input, 'keydown', 13) // simulate hitting enter 
    var match = /next: move to the next lesson/ 
    ok(match.test($('.terminal-output'), 'Expected to find "next: move to the next lesson"') 
}) 

現在,這一切完美的作品,如果我訪問的頁面,並手動輸入,但我想這個程序來完成。

歡迎任何建議!

+0

你有這個應用程序在網上嗎? – jcubic

+0

@jcubic沒有更多。我在最後建立https://github.com/mfeckie/try-ruby-ember-edition,但它沒有被使用太多,並沒有收到很長一段時間的愛 – muttonlamb

回答

1

這裏是將輸入文字,然後按輸入代碼:

function enter(text) { 
    var e; 
    var $root = $(document.documentElement || window); 
    for (var i=0; i<text.length; ++i) { 
     e = $.Event("keypress"); 
     e.which = text.charCodeAt(i); 
     $root.trigger(e); 
    } 
} 

注:的

terminal.insert("Hello"); 
var e = $.Event("keydown"); 
e.ctrlKey = false; 
e.which = e.keyCode = 13; 
$(document.documentElement || window).trigger(e); 

代替terminal::insert你也可以使用jQuery事件模擬輸入文字結合按鍵的代碼並且keydown在末尾處插入cmd插件:

$(document.documentElement || window).bind('keypress.cmd', function(e) { 

    ... 

}).bind('keydown.cmd', keydown_event); 

keydown_event是處理所有快捷方式和按鍵的主要功能,用於輸入文本。

如果你要測試的是終端最後的回聲文本內將是:

terminal.find('.terminal-output div:last').html() 

和前行,這將是你進入提示+文字(我要補充特定類的線)。

此外,如果您想處理終端格式化,您需要查看$ .terminal.format函數中的格式規則,它會爲每種格式的內聯樣式,類和屬性創建span。測試HTML而不是文本會給你更好的結果,我認爲。帶提示的行不會將其格式設置爲以文本形式顯示的跨度。

+0

只好添加'.replace(/  /g,「」)'到'.html()'來獲得與QUnit一起工作的匹配器,但除了那些可怕的東西。 – muttonlamb

+0

@muttonlamb終端編碼特殊字符,如果你想測試你輸入的文本是否在終端中相同,你應該使用'.text()',看$ .terminal.encode,'.html()'可能會更好地測試終端本身。 – jcubic