javascript 我得到“AssertionError:当我测试我的solidity代码时,预期事务将恢复为“Username

hyrbngr7  于 2023-05-12  发布在  Java
关注(0)|答案(3)|浏览(223)

当我在下面的smartcontract上运行npx hardhat test时,我会得到
Assert错误:预期要恢复的事务,用户名已被采用,请尝试另一个,但引发了其他异常:错误:处理事务时出现VM异常:已恢复,原因字符串为“用户名已被占用,请尝试另一个用户名。”
误差
这是我的实体合同

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import "hardhat/console.sol";

contract Ewitter{
    struct User{
        address wallet;
        string name;
        string username;
        string bio;
        string avatar;
    }

    mapping(address => string) public usernames;
    mapping(string => User) public users;

    function signup( string memory _username, string memory _name, string memory _bio, string memory _avatar) public{
        require(bytes(usernames[msg.sender]).length == 0, "User already exists");
        require(users[_username].wallet == address(0), "Username is taken, please try another one.");

        users[_username] = User({
            wallet: msg.sender,
            name: _name,
            username: _username,
            bio: _bio,
            avatar: _avatar
        });
        usernames[msg.sender] = _username;
    }

    function getUser(address _wallet) public view returns (User memory){
        return users[usernames[_wallet]];
    }
}

这是我的测试代码(test.js文件)

const { expect } = require("chai");
const { ethers } = require("hardhat");

describe("Ewitter", function () {
  it("Test ewitter signup flow", async function () {
    const Ewitter = await ethers.getContractFactory("Ewitter");
    const [user1, user2] = await  ethers.getSigners();
    const ewitter = await Ewitter.deploy(); //If you've set value of the string directly then you don't have to pass anything to the constructor
    await ewitter.deployed();

    await ewitter.signup("chirag", "Chirag", "Some bio", "someUrl");
    console.log("signing up user for chirag....");

    const user = await ewitter.users("chirag");
    expect(user.name).to.equal("Chirag");
    expect(user.bio).to.equal("Some bio");
    expect(user.avatar).to.equal("someUrl");
    console.log("test signup is successful");

    const userFromAddress = await ewitter.getUser(user1.address);
    expect(userFromAddress.username).to.equal("chirag")
    expect(userFromAddress.name).to.equal("Chirag");
    expect(userFromAddress.bio).to.equal("Some bio");
    expect(userFromAddress.avatar).to.equal("someUrl");
    console.log("test signup is successful")

    expect(await ewitter.usernames(user1.address)).to.equal("chirag");
    await expect(ewitter.signup("", "", "", "")).to.be.revertedWith(
      "User already exists"
    )

    await expect(ewitter.connect(user2).signup("chirag", "Chirag", "Some other bio", "someAvatar")).to.be.revertedWith(
      "Username is taken please try another one"
    )  
    console.log("test user already exists error")
  });
});

这是我得到的错误

qlvxas9a

qlvxas9a1#

你失踪了。在expect语句中的1之后

czq61nw1

czq61nw12#

您应该编写与原始脚本相同的还原消息:
原创剧本:

require(
        msg.value.getconversionRate(priceFeed) >= minimumUSD,
        "Didn't send enough!"
    );

测试脚本:

it.only("Fails if not sending enough ETH", async function () {
            await expect(fundMe.fund()).to.be.revertedWith(
                "Didn't send enough!"
            )
        })
pod7payv

pod7payv3#

这就是错误的来源

require(users[_username].wallet == address(0), "Username is taken, please try another one."

await expect(ewitter.connect(user2).signup("chirag", "Chirag", "Some other bio", "someAvatar")).to.be.revertedWith(
  "Username is taken please try another one"
)

而不是“用户已经存在”,它应该是
“用户名已被占用,请尝试其他用户名。”
所以我相信你的代码应该是这样的

await expect(ewitter.signup("", "", "", "")).to.be.revertedWith(
 "Username is taken, please try another one."
);

“用户名已被占用,请尝试另一个。”与“用户名已被占用,请尝试另一个”不同。

相关问题