存根回调函数
Stub a Function with Callback
通常我使用这个代码:
aws_stub.S3 = function(){};
var fake_aws_listObjects = function(params, func){func("failure", null)};
var fake_aws_listObjects_stub = sinon.spy(fake_aws_listObjects);
aws_stub.S3.prototype.listObjects = fake_aws_listObjects_stub;
存根函数,如下所示:
new AWS.S3().listObjects(that.build_params(), function(err, data) {
if(err){
that.make_failure_callback();
}
else{
that.add_keys(data);
if(data.IsTruncated){
that.make_request(data.NextMarker);
}else{
that.make_success_callback(that.keys);
}
}
});
这个存根的问题是每个请求它returns相同
现在我想为每个调用做不同的存根:
aws_stub.S3 = function(){};
var fake_aws_truncated = function(params, func){func(null, {
Contents: [{Key: "3fb252ba-0724-438c-93b6-8cd0fd972a8e/image/1:::2.jpg"}],
IsTruncated: true,
NextMarker: "nextMarker"
})};
var fake_aws_listObjects = function(params, func){func(null, {Contents: [{
Key: "3fb252ba-0724-438c-93b6-8cd0fd972a8e/image/2:::3.jpg"
}]})};
var fake_aws_listObjects_stub = sinon.stub();
fake_aws_listObjects_stub.onCall(0).returns(fake_aws_truncated);
fake_aws_listObjects_stub.onCall(1).returns(fake_aws_listObjects);
aws_stub.S3.prototype.listObjects = fake_aws_listObjects_stub;
问题好像是returns
,没有执行函数!!
我也不会这样写:
fake_aws_listObjects_stub.onCall(0) = fake_aws_truncated;
因为这会是一个错误的手写作业!
我需要更改什么?这是 sinon 文档:http://sinonjs.org/docs/
谢谢!!
我可能会换一种方式。
如果你想存根 AWS.S3.prototype.listObjects
,我会这样做:
var stub = sinon.stub(AWS.S3.prototype, 'listObjects');
要使用各种值调用回调,请使用 stub.yields()
:
stub.onCall(0).yields(null, {
Contents : [{Key: "3fb252ba-0724-438c-93b6-8cd0fd972a8e/image/1:::2.jpg"}],
IsTruncated : true,
NextMarker : "nextMarker"
});
stub.onCall(1).yields(null, {
Contents : [{Key: "3fb252ba-0724-438c-93b6-8cd0fd972a8e/image/2:::3.jpg"}]
});
要测试您的代码,您只需像以前一样调用 listObjects
:
var s3 = new AWS.S3();
s3.listObjects(params, function(err, value) {
...`value` is now one of the fixtures you declared above...
});
要恢复到原始版本的方法,请使用以下之一:
stub.restore();
// OR:
AWS.S3.prototype.listObjects.restore();
通常我使用这个代码:
aws_stub.S3 = function(){};
var fake_aws_listObjects = function(params, func){func("failure", null)};
var fake_aws_listObjects_stub = sinon.spy(fake_aws_listObjects);
aws_stub.S3.prototype.listObjects = fake_aws_listObjects_stub;
存根函数,如下所示:
new AWS.S3().listObjects(that.build_params(), function(err, data) {
if(err){
that.make_failure_callback();
}
else{
that.add_keys(data);
if(data.IsTruncated){
that.make_request(data.NextMarker);
}else{
that.make_success_callback(that.keys);
}
}
});
这个存根的问题是每个请求它returns相同
现在我想为每个调用做不同的存根:
aws_stub.S3 = function(){};
var fake_aws_truncated = function(params, func){func(null, {
Contents: [{Key: "3fb252ba-0724-438c-93b6-8cd0fd972a8e/image/1:::2.jpg"}],
IsTruncated: true,
NextMarker: "nextMarker"
})};
var fake_aws_listObjects = function(params, func){func(null, {Contents: [{
Key: "3fb252ba-0724-438c-93b6-8cd0fd972a8e/image/2:::3.jpg"
}]})};
var fake_aws_listObjects_stub = sinon.stub();
fake_aws_listObjects_stub.onCall(0).returns(fake_aws_truncated);
fake_aws_listObjects_stub.onCall(1).returns(fake_aws_listObjects);
aws_stub.S3.prototype.listObjects = fake_aws_listObjects_stub;
问题好像是returns
,没有执行函数!!
我也不会这样写:
fake_aws_listObjects_stub.onCall(0) = fake_aws_truncated;
因为这会是一个错误的手写作业!
我需要更改什么?这是 sinon 文档:http://sinonjs.org/docs/
谢谢!!
我可能会换一种方式。
如果你想存根 AWS.S3.prototype.listObjects
,我会这样做:
var stub = sinon.stub(AWS.S3.prototype, 'listObjects');
要使用各种值调用回调,请使用 stub.yields()
:
stub.onCall(0).yields(null, {
Contents : [{Key: "3fb252ba-0724-438c-93b6-8cd0fd972a8e/image/1:::2.jpg"}],
IsTruncated : true,
NextMarker : "nextMarker"
});
stub.onCall(1).yields(null, {
Contents : [{Key: "3fb252ba-0724-438c-93b6-8cd0fd972a8e/image/2:::3.jpg"}]
});
要测试您的代码,您只需像以前一样调用 listObjects
:
var s3 = new AWS.S3();
s3.listObjects(params, function(err, value) {
...`value` is now one of the fixtures you declared above...
});
要恢复到原始版本的方法,请使用以下之一:
stub.restore();
// OR:
AWS.S3.prototype.listObjects.restore();