我如何存根 @ra.do_post 以便它进入“步骤 2”
- 现在,当我在第 1 步之后在 Async.Waterfall 中运行我的规范时,它会进入第 5 步
- 我认为在我的规范中我没有正确返回回调以使其进入第 2 步
滚动到最后看看我在做什么。提前感谢一堆
RA = require('./request_adapter')
class QueryHandler
constructor: (@adapter) ->
@ra = new RA()
create_test: (id, data, cb) ->
self = this
_url = url + adapter_apis.CREATE
bll_url = "http://localhost:" + 11 + "/" + bll_apis.PKG_SWITCH
Async.waterfall [
(done) ->
console.info("Step 1 ==================")
@ra.do_post some_url, data, done
, (resp, done) ->
console.info("Step 2 ==================")
console.info(resp)
if resp? and resp.status isnt "SUCCESS"
console.info("Step 3a ==================")
cb({error: true, message: "error."})
else
console.info("Step 3b ==================")
fn = () -> self.ra.do_post _url, data , done
helper.delay 500, fn
, (resp, done) ->
console.info("Step 4 ==================")
cb(null,resp)
], (error, resp) ->
console.info("Step 5 ==================")
if error
cb({error: true, message: "errored out"})
这是我在规范中尝试的
var qh = require(query_handler");
var qh_obj = new qh("FAKE");
var fn = function(ccb){
ccb(undefined, 10);
};
stub = sinon.stub(qh_obj.ra,'do_post').returns(fn);
qh_obj.create_test("fake_id", {switch_data: {a: 11}}, function(e,r) {
console.log(e);
(e.error).should.be.true
done();
});