2014-07-13 48 views
0

我有一些使用像黃瓜和生菜這樣的BDD工具的經驗。我目前正在構建一個Phonegap應用程序,並且我想開始使用Cucumber.js爲它創建驗收測試。不幸的是我有一個問題。ZombieJS在Cucumber.js場景中的回調問題

下面是基本功能的文件,我扔在一起:

Feature: Authentication 

    As a user 
    I want to be able to log in and out 

    Scenario: Logging in 
     Given I am not logged in 
     And I am on the page "login" 
     When I fill in the "username" field with "student" 
     And I fill in the "password" field with "password" 
     And I click the "LOG IN" button 
     Then I should see the text "STUDENT" 

這是我world.js

var zombie = require('zombie'); 
var World = function World(callback) { 
    "use strict"; 

    this.browser = new zombie(); // this.browser will be available in step definitions 

    this.visit = function (url, callback) { 
     this.browser.visit(url, callback); 
    }; 

    callback(); // tell Cucumber we're finished and to use 'this' as the world instance 
}; 
exports.World = World; 

這裏是我的步驟定義:

var wrapper = function() { 
    "use strict"; 

    this.World = require("../support/world.js").World; // overwrite default World constructor 

    this.Given(/^I am not logged in$/, function (callback) { 
     // Clear local storage 
     this.browser.localStorage("localhost:9001").clear(); 
     callback(); 
    }); 

    this.Given(/^I am on the page "([^"]*)"$/, function (page, callback) { 
     // Visit page 
     this.browser.visit('http://localhost:9001/app/index.html#' + page, callback); 
    }); 
}; 

module.exports = wrapper; 

我已經建立了一個Grunt任務,首先在端口9001上運行連接服務器,然後運行Cucumber場景秒。 documentation for Cucumber.js implies this should work,但它在第二步失敗。

以下是錯誤消息我得到:

Running "connect:cucumber" (connect) task 
Started connect web server on http://localhost:9001 

Running "cucumberjs:src" (cucumberjs) task 
.Cannot call method 'add' of undefined TypeError: Cannot call method 'add' of undefined 
    at <anonymous>:10:711 
    at <anonymous>:10:874 
    at <anonymous>:10:1224 
    at Contextify.sandbox.run (/Users/matthewdaly/Projects/myapp/node_modules/zombie/node_modules/jsdom/node_modules/contextify/lib/contextify.js:12:24) 
    at DOMWindow.window._evaluate (/Users/matthewdaly/Projects/myapp/node_modules/zombie/lib/zombie/window.js:188:25) 
    at Object.HTML.languageProcessors.javascript (/Users/matthewdaly/Projects/myapp/node_modules/zombie/lib/zombie/scripts.js:23:21) 
    at define.proto._eval (/Users/matthewdaly/Projects/myapp/node_modules/zombie/node_modules/jsdom/lib/jsdom/level2/html.js:1480:47) 
    at loaded (/Users/matthewdaly/Projects/myapp/node_modules/zombie/lib/zombie/scripts.js:74:23) 
    at /Users/matthewdaly/Projects/myapp/node_modules/zombie/node_modules/jsdom/lib/jsdom/level2/html.js:76:20 
    at Object.item.check (/Users/matthewdaly/Projects/myapp/node_modules/zombie/node_modules/jsdom/lib/jsdom/level2/html.js:345:11) 
FUUUU 

(::) failed steps (::) 

TypeError: Cannot call method 'add' of undefined 
    at <anonymous>:10:711 
    at <anonymous>:10:874 
    at <anonymous>:10:1224 
    at Contextify.sandbox.run (/Users/matthewdaly/Projects/myapp/node_modules/zombie/node_modules/jsdom/node_modules/contextify/lib/contextify.js:12:24) 
    at DOMWindow.window._evaluate (/Users/matthewdaly/Projects/myapp/node_modules/zombie/lib/zombie/window.js:188:25) 
    at Object.HTML.languageProcessors.javascript (/Users/matthewdaly/Projects/myapp/node_modules/zombie/lib/zombie/scripts.js:23:21) 
    at define.proto._eval (/Users/matthewdaly/Projects/myapp/node_modules/zombie/node_modules/jsdom/lib/jsdom/level2/html.js:1480:47) 
    at loaded (/Users/matthewdaly/Projects/myapp/node_modules/zombie/lib/zombie/scripts.js:74:23) 
    at /Users/matthewdaly/Projects/myapp/node_modules/zombie/node_modules/jsdom/lib/jsdom/level2/html.js:76:20 
    at Object.item.check (/Users/matthewdaly/Projects/myapp/node_modules/zombie/node_modules/jsdom/lib/jsdom/level2/html.js:345:11) 

如果我的第二個步驟後體內插入callback();,它傳遞。我不確定發生了什麼事。爲什麼這種情況會失敗?該應用程序本身按預期工作。看起來第二步的回調似乎永遠不會發射。

回答

0

如果您將回調添加到第二步,測試通過,因爲只是跳過了visitPage。

我訪問函數如下:

this.visit = function(url, callback) { 
    that.browser.visit(url, function(error) { 
    if (error) { 
     callback.fail(error); 
    } else { 
     callback.call(that, that.browser); 
    } 
    }); 
}); 

,但我認爲真正的問題是,您的網頁上,因爲sandbox.run就是殭屍開始從頁面執行自定義(JS)碼點。所以這是1224列中的(縮小)腳本中的匿名回調? 也許你必須用console.log來跟蹤它(有localStorage的東西嗎?allthough zombie支持它),grep for'add'在你的自定義代碼中