2017-08-15 30 views
0

我想写这样一个特点:黄瓜Js回调问题?或功能问题?

Scenario: new Singleton create 
    When a new, unmatchable identity is received 
    Then a new tin record should be created 
    And a new bronze record should be created 
    And a new gold record should be created 

这将绑步骤是这样的:

defineSupportCode(function ({ Before, Given, Then, When }) { 

    var expect = require('chai').expect; 
    var chanceGenerator = require('./helpers/chanceGenerator') 
    var request = require('./helpers/requestGenerator') 

    let identMap; 

    // reset identMap before each scenario 
    Before(function() { 
    identMap = []; 
    }); 

    // should generate a valid identity 
    // persist it in a local variable so it can be tested in later steps 
    // and persist to the db via public endpoint 
    When('a new, unmatchable identity is received', function (callback) { 
    identMap.push(chanceGenerator.identity()); 
    request.pubPostIdentity(identMap[identMap.length-1], callback); 
    }); 

    // use the local variable to retrieve Tin that was persisted 
    // validate the tin persisted all the props that it should have 
    Then('a new tin record should be created', function (callback) { 
    request.pubGetIdentity(identMap[identMap.length-1], callback); 

    // var self = this; 

    // request.pubGetIdentity(identMap[identMap.length-1], callback,() => { 
    // console.log('never gets here...'); 
    // self.callback(); 
    // callback(); 
    // }); 

    // request.pubGetIdentity(identMap[identMap.length-1], (callback) => { 
    // console.log('never gets here...'); 
    // self.callback(); 
    // callback(); 
    // }); 

    }); 

是我遇到的问题是,我不能做任何事情在Then回调中。这就是我希望能够验证响应是否具有正确数据的地方。

这里是从辅助文件的有关摘录:我们正在考虑作为一个选项是重新写的功能,以适应不同的步骤定义结构

var pubPostIdentity = function (ident, callback) { 
    console.log('pubIdentity'); 
    var options = { 
    method: 'POST', 
    url: 'http://cucumber.utu.ai:4020/identity/' + ident.platform + '/' + ident.platformId, 
    headers: { 
     'X-Consumer-Custom-Id': ident.botId + '_' + ident.botId 
    }, 
    body: JSON.stringify(ident) 
    }; 
    console.log('ident: ', ident); 
    request(options, (err, response, body) => { 
    if (err) { 
     console.log('pubPostIdentity: ', err); 
     callback(err); 
    } 
    console.log('pubPostIdentity: ', response.statusCode); 
    callback(); 
    }); 
} 

// accept an identity and retrieve from staging via identity public endpoint 
var pubGetIdentity = function (ident, callback) { 
    console.log('pubGetIdentity'); 
    var options = { 
    method: 'GET', 
    url: 'http://cucumber.utu.ai:4020/identity/' + ident.platform + '/' + ident.platformId, 
    headers: { 
     'X-Consumer-Custom-Id': ident.botId + '_' + ident.botId 
    } 
    }; 
    request(options, (err, response) => { 
    if (err) { 
     console.log('pubGetIdentity: ', err); 
     callback(err); 
    } 
    console.log('pubGetIdentity: ', response.body); 
    callback(); 
    }); 
} 

东西。如果我们重新写了这样的功能:

Scenario: new Singleton create 
    When a new, unmatchable 'TIN_RECORD' is received 
    Then the Identity Record should be created successfully 
    When the Identity Record is retreived for 'tin' 
    Then a new 'tin' should be created 
    When the Identity Record is retreived for 'bronze' 
    Then a new 'bronze' should be created 
    When the Identity Record is retreived for 'gold' 
    Then a new 'gold' should be created 

我相信它绕过我们与摔跤脚背回调的问题,但我真的很讨厌这个功能的细分。它使该功能对业务不易读和易于理解。

所以......我的问题,首先提出的摘要功能是否写错了?我是否试图让步骤定义去做一些他们不应该做的事情?或者我的Js技能缺乏光明,这应该是非常可行的,我只是搞砸了回调?

回答

0

首先,我会说你的重写功能是错误的。你永远不应该回到给定的时间,当,然后。你要从那时回到当时,这是错误的。

鉴于用于设置先决条件。何时用于实际测试。然后用于断言。每个场景应该是单个测试,所以应该有很少的When子句。如果你愿意,你可以使用Scenario Outlines将几个非常相似的测试混合在一起。

在这种情况下,建议回到第一原则,看看是否有效。然后慢慢建立起来工作。

我怀疑在这种情况下,问题是在一些异常被抛出,不处理。你可以尝试重写它来使用promise,然后错误地被拒绝。这样可以更好地报告错误。

+0

格雷厄姆感谢您的回复!我摔跤w /几个想法:首先,如果总是/只是要求声明?然后Then处理评估来自When/request的反馈?如果是这种情况,那么我们试图测试的场景将遵循WhenThenWhenThen ...模式(我讨厌)。我想要这样做的是有一个Then然后阻止并使多个Rest请求来验证When中生成的数据。那是犹太教吗?我可以有一个阻塞然后,这使得多个外部请求调用?真的很感谢反馈! –