如何将论点作为对象进行比较?
我正在使用Sinon Stub嘲笑第三方的回应。一切都很好,因为我需要检查调用函数的输入。
示例:我创建一个函数以获取用户信息,使用猫鼬从mongodb获取数据。
async function getUser(userId) {
const userInfo = await User.findOne({ _id: userId });
return userInfo;
}
我的UT:
describe('Test user', () => {
let findOneUserStub;
beforeEach(() => {
findOneUserStub = sinon.stub(User, 'findOne');
});
afterEach(() => {
findOneUserStub.restore();
});
// Work
it('Should return information success', async () => {
const response = await getUser('userId_01');
findOneUserStub.returns({ _id: 'userId_01'});
expect(response).to.be.equals({ _id: 'userId_01'});
});
// Not work
it('Should return information success', async () => {
const response = await getUser('userId_01');
findOneUserStub.withArgs({ _id: 'userId_01'}).returns({ _id: 'userId_01'});
expect(response).to.be.equals({ _id: 'userId_01'});
});
})
我认为它只能与值变量进行比较,不能与参考变量进行比较。那么,我们有什么方法可以比较参考变量吗?
I'm using sinon stub to mock the response from 3rd party. Everything is fine since I need to check the input of function called.
Example: I create a function to get user information, use Mongoose to get data from mongodb.
async function getUser(userId) {
const userInfo = await User.findOne({ _id: userId });
return userInfo;
}
My UT:
describe('Test user', () => {
let findOneUserStub;
beforeEach(() => {
findOneUserStub = sinon.stub(User, 'findOne');
});
afterEach(() => {
findOneUserStub.restore();
});
// Work
it('Should return information success', async () => {
const response = await getUser('userId_01');
findOneUserStub.returns({ _id: 'userId_01'});
expect(response).to.be.equals({ _id: 'userId_01'});
});
// Not work
it('Should return information success', async () => {
const response = await getUser('userId_01');
findOneUserStub.withArgs({ _id: 'userId_01'}).returns({ _id: 'userId_01'});
expect(response).to.be.equals({ _id: 'userId_01'});
});
})
I think it only able compare with value variable, is cannot compare with reference variable. So do we have any way to compare reference variable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您可以使用以下方式:
httpps://sinonjs.s.org/releases/latest/latest/assertions/latest/assertions/
I think you can use this:
https://sinonjs.org/releases/latest/assertions/