Truffle 单元案例的 Truffle Chai 断言错误
Truffle Chai Assertion Error with Truffle Unit Cases
问题:在我的合约中尝试实施 ERC20 令牌时遇到以下 Truffle 测试用例的问题。
contract("Token Test", async(accounts) => {
const[deployerAccount, recipient, anotherAccount] = accounts;
it("All tokens should be in deployer's account", async() => {
let instance = await Token.deployed();
let totalSupply = await instance.totalSupply();
// let balance = await instance.balanceOf(accounts[0]);
// assert.equal(balance.valueOf(), totalSupply.valueOf(), "The Balance Was not same");
expect(instance.balanceOf(deployerAccount)).to.eventually.be.a.bignumber.equal(totalSupply);
});
it("Is possible to send tokens between accounts", async() => {
const sendToken = 1;
let instance = await Token.deployed();
let totalSupply = await instance.totalSupply();
expect(instance.balanceOf(deployerAccount)).to.eventually.be.a.bignumber.equal(totalSupply);
expect(instance.transfer(recipient, sendToken)).to.eventually.be.fulfilled;
expect(instance.balanceOf(deployerAccount)).to.eventually.be.a.bignumber.equal(totalSupply.sub(new BN(sendToken)));
expect(instance.balanceOf(recipient)).to.eventually.be.a.bignumber.equal(new BN(sendToken));
});
it("Should not be able to send more token than available with owner", async() => {
let instance = await Token.deployed();
let balanceOfDeployer = await instance.balanceOf(deployerAccount);
console.log(balanceOfDeployer+ " : " + await instance.balanceOf(deployerAccount));
// expect(instance.transfer(recipient, new BN(balanceOfDeployer+3))).to.eventually.be.rejected;
expect(instance.balanceOf(deployerAccount)).to.eventually.be.a.bignumber.equal(balanceOfDeployer);
});
});
描述:当我尝试同时执行测试用例 2 和 3 时失败并出现以下错误:
"before all" hook: prepare suite for "Should not be able to send more token than available with owner":
Uncaught AssertionError: expected '1000000000' to equal '999999999'
+ expected - actual
-1000000000
+999999999
at /project_dir/node_modules/chai-as-promised/lib/chai-as-promised.js:302:22
at processTicksAndRejections (internal/process/task_queues.js:95:5)
UnhandledRejections detected
Promise {
<rejected> AssertionError: expected '1000000000' to equal '999999999'
at /project_dir/node_modules/chai-as-promised/lib/chai-as-promised.js:302:22
at processTicksAndRejections (internal/process/task_queues.js:95:5) {
showDiff: true,
actual: '1000000000',
expected: '999999999',
operator: 'strictEqual',
uncaught: true
}
然而,当我尝试独立执行两个测试用例时(评论测试用例 2 保留 3,反之亦然)。它们工作正常并且没有任何错误地完成。
需要帮助了解我在这里缺少什么以及如何修复它。
我尝试过的事情:
最初,我认为这可能是由于测试用例 2 中变量的状态变化而发生的。所以我将测试用例 3 移动到一个新的 contract() 中。但我仍然面临同样的问题。然而,这不应该发生,因为 contract() 在执行每个合约测试之前提供了一个干净的房间环境。
P.S.: 我在 Contract 的构造函数中将 totalSupply 的值初始化为 1000000000。
在每个 expect()
我试图与合约通信的地方之前添加等待。这有助于解决问题。
问题:在我的合约中尝试实施 ERC20 令牌时遇到以下 Truffle 测试用例的问题。
contract("Token Test", async(accounts) => {
const[deployerAccount, recipient, anotherAccount] = accounts;
it("All tokens should be in deployer's account", async() => {
let instance = await Token.deployed();
let totalSupply = await instance.totalSupply();
// let balance = await instance.balanceOf(accounts[0]);
// assert.equal(balance.valueOf(), totalSupply.valueOf(), "The Balance Was not same");
expect(instance.balanceOf(deployerAccount)).to.eventually.be.a.bignumber.equal(totalSupply);
});
it("Is possible to send tokens between accounts", async() => {
const sendToken = 1;
let instance = await Token.deployed();
let totalSupply = await instance.totalSupply();
expect(instance.balanceOf(deployerAccount)).to.eventually.be.a.bignumber.equal(totalSupply);
expect(instance.transfer(recipient, sendToken)).to.eventually.be.fulfilled;
expect(instance.balanceOf(deployerAccount)).to.eventually.be.a.bignumber.equal(totalSupply.sub(new BN(sendToken)));
expect(instance.balanceOf(recipient)).to.eventually.be.a.bignumber.equal(new BN(sendToken));
});
it("Should not be able to send more token than available with owner", async() => {
let instance = await Token.deployed();
let balanceOfDeployer = await instance.balanceOf(deployerAccount);
console.log(balanceOfDeployer+ " : " + await instance.balanceOf(deployerAccount));
// expect(instance.transfer(recipient, new BN(balanceOfDeployer+3))).to.eventually.be.rejected;
expect(instance.balanceOf(deployerAccount)).to.eventually.be.a.bignumber.equal(balanceOfDeployer);
});
});
描述:当我尝试同时执行测试用例 2 和 3 时失败并出现以下错误:
"before all" hook: prepare suite for "Should not be able to send more token than available with owner":
Uncaught AssertionError: expected '1000000000' to equal '999999999'
+ expected - actual
-1000000000
+999999999
at /project_dir/node_modules/chai-as-promised/lib/chai-as-promised.js:302:22
at processTicksAndRejections (internal/process/task_queues.js:95:5)
UnhandledRejections detected
Promise {
<rejected> AssertionError: expected '1000000000' to equal '999999999'
at /project_dir/node_modules/chai-as-promised/lib/chai-as-promised.js:302:22
at processTicksAndRejections (internal/process/task_queues.js:95:5) {
showDiff: true,
actual: '1000000000',
expected: '999999999',
operator: 'strictEqual',
uncaught: true
}
然而,当我尝试独立执行两个测试用例时(评论测试用例 2 保留 3,反之亦然)。它们工作正常并且没有任何错误地完成。
需要帮助了解我在这里缺少什么以及如何修复它。
我尝试过的事情: 最初,我认为这可能是由于测试用例 2 中变量的状态变化而发生的。所以我将测试用例 3 移动到一个新的 contract() 中。但我仍然面临同样的问题。然而,这不应该发生,因为 contract() 在执行每个合约测试之前提供了一个干净的房间环境。
P.S.: 我在 Contract 的构造函数中将 totalSupply 的值初始化为 1000000000。
在每个 expect()
我试图与合约通信的地方之前添加等待。这有助于解决问题。