如何解决TypeScript节点etcd3事务模拟
我当时正在考虑模拟etcd3来运行单元测试。
它似乎不适用于事务处理-如果我也将模拟代码注释掉,它也将不起作用。
这是基于etcd3交易示例
import React,{ useState,useEffect } from "react";
import { Doughnut,Line,Pie } from "react-chartjs-3";
export default function UserDashboard() {
const DoughData = {
labels: ["Red","Green","Yellow"],datasets: [
{
data: [300,50,100],backgroundColor: ["#FF6384","#36A2EB","#FFCE56"],hoverBackgroundColor: ["#FF6384",},],};
return (
<div>
<Doughnut data={DoughData} />
</div>
);
}
这给出了
import 'jasmine';
import { Etcd3 } from 'etcd3';
import sinon from 'sinon';
const amount = 3;
const etcd3 = new Etcd3();
// const mock = etcd3.mock({ exec: sinon.stub() } as any);
// mock.exec.resolves({ kvs: [{ key: 'foo',value: amount.toString() }] });
describe('transaction',() => {
it('mocks',async () => {
await Promise.all([
etcd3.put('bank/account1').value(20),etcd3.put('bank/account2').value(20)])
await etcd3.stm().transact(tx => {
return Promise.all([
tx.get('bank/account1').number(),tx.get('bank/account2').number(),])
.then(([balance1,balance2]) => {
if (balance1 && balance2) {
return Promise.all([
tx.put('bank/account1').value(balance1 - amount),tx.put('bank/account2').value(balance2 + amount),])
}
})
})
});
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。