![](/img/trans.png)
[英]Vows.js: Accessing parameters returned from outer topics inside inner topics
[英]What is wrong with my usage of vows.js sub-topics?
出於某種原因,我似乎無法在真正的測試套件中使用vows.js子主題,但是在示例文件中它們可以正常工作……您能發現我的問題嗎?
這有效:
vows.describe('An Education in Vows').addBatch({
'when I write an asynchronous topic': {
topic: function() {
var that = this;
setTimeout(function() {
that.callback(true);
}, 100);
},
'it passes': function (topic) {
assert.equal(topic, true);
},
'and it has an asynchronous sub-topic': {
topic: function() {
var that = this;
setTimeout(function() {
that.callback(true);
}, 100);
},
'it also passes': function (topic) {
assert.equal(topic, true);
}
}
}
}).run();
當我通過以下方式運行它時:
node tests/learning-vows.js
我得到:
·
·
✓ OK » 2 honored (0.207s)
這不起作用:
我有一個文件./tests/smoke.js
vows.describe('Registration & Authentication').addBatch({
'when a user registers with a valid username and password': {
topic: function () {
client.register({
username: validusername,
password: validpassword
}, this.callback);
},
'we return status 200 OK': function (data, response) {
assert.equal(200, response.statusCode);
},
'simple sub-topic': {
topic: true,
'should work': function(topic) {
assert.equal(true, topic);
}
},
}
}).run()
當我通過以下方式執行此操作時:
node tests/smoke.js
我得到:
·
✗ Errored » 1 honored ∙ 1 errored
請注意,在第二個示例中,沒有子主題,我得到:
·
✓ OK » 1 honored (0.100s)
Vows使用節點的約定進行回調(請參閱: http : //nodemanual.org/latest/nodejs_dev_guide/working_with_callbacks.html ),它假定回調的第一個參數是一個錯誤對象。
因此,當您將data
作為第一個參數發送時,您的誓言誓言client.register
發生了錯誤。 它防止誓言評估子主題。 子主題被標記為錯誤,但是斷言成功,當前主題被標記為已接受。
從輸出中猜測出來確實不是一件容易的事。 此外,誓言行為不一致,請在第一個測試中嘗試將true
替換為0
,然后將'0'
用作回調參數,您將看到另外兩個結果。
這是一個工作示例:
var vows = require('vows'), assert = require('assert');
var client = {
register: function(obj,callback){
callback(null, obj, {statusCode:200});
}
};
vows.describe('Registration & Authentication').addBatch({
'when a user registers with a valid username and password': {
topic: function () {
client.register({
username: 'validusername',
password: 'validpassword'
}, this.callback);
},
'we return status 200 OK': function (err, data, response) {
assert.equal(response.statusCode, 200);
},
'simple sub-topic': {
topic: true,
'should work': function(topic) {
assert.equal(true, topic);
}
}
}
}).export(module)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.