TypeError: stub expected to yield, but no callback was passed. Received [[object Object]] while using sinon to mock a dynamodb get call
TypeError: stub expected to yield, but no callback was passed. Received [[object Object]] while using sinon to mock a dynamodb get call
我有一个简单的 lambda 函数,它可以调用 dynamodb。我正在尝试使用 sinon 来模拟这个,但遇到了一个错误。
app.js
var AWS = require('aws-sdk');
AWS.config.update({region: 'us-east-1'});
async function run(){
const dynamoDB = new AWS.DynamoDB.DocumentClient();
const params = {
TableName: 'employees',
Key: {
name: "naxi"
}
};
const result = await dynamoDB.get(params).promise();
if (result.Item) {
return result.Item;
} else {
return { error: 'Task not found.' };
}
}
module.exports = {
run
}
index.spec.js
const sinon = require('sinon');
const assert = require('assert');
const proxyquire = require('proxyquire');
describe('Test Suite', () => {
let mut;
let clientStub;
before(() => {
clientStub = {
get: sinon.stub()
}
const awsStub = {
DynamoDB: {
DocumentClient: class {
constructor() {
return clientStub;
}
}
}
}
mut = proxyquire('../app.js', {
'aws-sdk': awsStub
})
});
it('should get random data!', async () => {
const expectedData = "neeraj";
clientStub.get.yields(null, expectedData);
const data = await mut.run();
sinon.assert.callCount(clientStub.get, 1);
assert.strictEqual(data, expectedData);
});
})
Package.json
"scripts": {
"test": "mocha ../**/*spec.js --recursive --timeout 10000"
}
一旦我 运行 测试脚本,我收到以下错误。
TypeError: stub expected to yield, but no callback was passed. Received [[object Object]]
任何人都可以告诉我我在这里缺少什么吗?
这就是我使用 supertest 包和 jest.
测试我的代码的方式
在我的后端,我正在发出 post 发送一些数据的请求。当然你可以根据需要跳过
let sandbox;
describe('Test', () => {
beforeEach(() => {
sandbox = sinon.createSandbox();
// process.env.redirectBaseUrl = 'http://xxxx/'; // if you want to set
});
afterEach(() => {
delete process.env.redirectBaseUrl;
sandbox.restore();
});
test('should return converted url', async () => {
const data = {
Item: {
convertedUrl: 'abc',
},
};
sandbox
.stub(AWS.DynamoDB.DocumentClient.prototype, 'get')
.returns({ promise: () => data });
const app = require('../app');
const response = await request(app)
.post('/users/anon-user/urls')
.send({ originalUrl: 'https://google.com' });
expect(response.body).toMatchObject({ convertedUrl: 'abc' });
expect(response.statusCode).toBe(201);
});
})
下面是我是如何让它工作的。
const sinon = require('sinon');
const assert = require('assert');
const proxyquire = require('proxyquire');
var AWS = require('aws-sdk');
let sandbox;
describe('Test', () => {
beforeEach(() => {
sandbox = sinon.createSandbox();
});
afterEach(() => {
sandbox.restore();
});
it('should return something', async () => {
const data = {
Item: {
convertedUrl: 'abc',
}
};
sandbox
.stub(AWS.DynamoDB.DocumentClient.prototype, 'get')
.returns({ promise: () => data });
const app = require('../app');
const result = await app.run();
assert.strictEqual(result, data);
});
});
我有一个简单的 lambda 函数,它可以调用 dynamodb。我正在尝试使用 sinon 来模拟这个,但遇到了一个错误。
app.js
var AWS = require('aws-sdk');
AWS.config.update({region: 'us-east-1'});
async function run(){
const dynamoDB = new AWS.DynamoDB.DocumentClient();
const params = {
TableName: 'employees',
Key: {
name: "naxi"
}
};
const result = await dynamoDB.get(params).promise();
if (result.Item) {
return result.Item;
} else {
return { error: 'Task not found.' };
}
}
module.exports = {
run
}
index.spec.js
const sinon = require('sinon');
const assert = require('assert');
const proxyquire = require('proxyquire');
describe('Test Suite', () => {
let mut;
let clientStub;
before(() => {
clientStub = {
get: sinon.stub()
}
const awsStub = {
DynamoDB: {
DocumentClient: class {
constructor() {
return clientStub;
}
}
}
}
mut = proxyquire('../app.js', {
'aws-sdk': awsStub
})
});
it('should get random data!', async () => {
const expectedData = "neeraj";
clientStub.get.yields(null, expectedData);
const data = await mut.run();
sinon.assert.callCount(clientStub.get, 1);
assert.strictEqual(data, expectedData);
});
})
Package.json
"scripts": {
"test": "mocha ../**/*spec.js --recursive --timeout 10000"
}
一旦我 运行 测试脚本,我收到以下错误。
TypeError: stub expected to yield, but no callback was passed. Received [[object Object]]
任何人都可以告诉我我在这里缺少什么吗?
这就是我使用 supertest 包和 jest.
测试我的代码的方式在我的后端,我正在发出 post 发送一些数据的请求。当然你可以根据需要跳过
let sandbox;
describe('Test', () => {
beforeEach(() => {
sandbox = sinon.createSandbox();
// process.env.redirectBaseUrl = 'http://xxxx/'; // if you want to set
});
afterEach(() => {
delete process.env.redirectBaseUrl;
sandbox.restore();
});
test('should return converted url', async () => {
const data = {
Item: {
convertedUrl: 'abc',
},
};
sandbox
.stub(AWS.DynamoDB.DocumentClient.prototype, 'get')
.returns({ promise: () => data });
const app = require('../app');
const response = await request(app)
.post('/users/anon-user/urls')
.send({ originalUrl: 'https://google.com' });
expect(response.body).toMatchObject({ convertedUrl: 'abc' });
expect(response.statusCode).toBe(201);
});
})
下面是我是如何让它工作的。
const sinon = require('sinon');
const assert = require('assert');
const proxyquire = require('proxyquire');
var AWS = require('aws-sdk');
let sandbox;
describe('Test', () => {
beforeEach(() => {
sandbox = sinon.createSandbox();
});
afterEach(() => {
sandbox.restore();
});
it('should return something', async () => {
const data = {
Item: {
convertedUrl: 'abc',
}
};
sandbox
.stub(AWS.DynamoDB.DocumentClient.prototype, 'get')
.returns({ promise: () => data });
const app = require('../app');
const result = await app.run();
assert.strictEqual(result, data);
});
});