2013-04-20 29 views
0

我使用zombiejs +摩卡編寫操縱的DOM庫的一些測試中,它使用JavaScript原生功能,如使用document.createElement(),我的問題是,當我運行測試我得到一個異常說'文檔沒有定義'。摩卡+ ZombieJs,文件沒有規定

是它zombiejs問題或與jsDOM?我該如何解決它?

這裏是我的代碼示例:

HTML:

<!DOCTYPE html> 

<html> 
    <head> 
    <script src="mocha.js"></script> 
    <script src="expect.js"></script> 
    <title>XS UI Tests</title> 

    <script type="text/javascript" charset="utf-8" src="../lib/mylibrary.js"></script> 

    <script> 
     mocha.setup('bdd'); 
    </script> 
    </head> 

    <body> 
    <div id="table"></div> 
    <div id="controls"></div> 
    <div id="form"></div> 

    <div id="mocha"></div> 

    <script src="tests.js"></script> 
    </body> 
</html> 

在tests.js

var expect = require('expect.js') 
    , Zombie = require('zombie' ) 
    , browser = new Zombie({ debug: true }) 
; 

describe('XS UI Tests:', function() { 
    before(function(done) { 
    browser.visit('http://localhost:8080/test/ui.html', done); 
    }); 

    it('expect ui.html to be loaded', function() { 
    expect(browser.success).to.be(true); 
    }); 

    describe('Table Tests:', function() { 
    it('expect div#table (table container) to exist', function() { 
     expect(browser.query("#table")).to.be.ok(); 
    }); 
    }); 
}); 

謝謝您的幫助

回答

1

document對象包含在殭屍瀏覽器中。如果你的代碼沒有找到document,很可能你沒有在殭屍沙箱上下文中運行它。

你加載通過zombie.visit('/page/with/library')您的庫或者是你直接要求它,並試圖在測試中使用它?

這是最好的測試完整網頁,而不是庫。您應該創建一個測試頁面,其上有幾個按鈕/鏈接,用於測試庫的不同功能,以及pressButtonvisit有殭屍的測試頁面。

+0

非常感謝您的回答。 確實我在我的測試腳本中通過'require'加載我的庫。 我只是修改我的代碼來加載我直接在HTML中測試的庫,但它不加載! – Khalifa 2013-05-20 18:01:10

+0

@Khalifa確保它在瀏覽器中,然後嘗試加入一些調試語句殭屍API,特別是在[資源](https://github.com/assaf/zombie/blob/master/src/zombie/resources .coffee)對象。 – gcochard 2013-05-21 02:00:33

+0

在瀏覽器中工作,沒問題,我已經在我的庫中調試消息,似乎我的庫不加載! – Khalifa 2013-05-21 17:27:41