【问题标题】:I am getting "AssertionError: Expected transaction to be reverted with Username " when I am testing my solidity code当我测试我的可靠性代码时,我收到“AssertionError: Expected transaction to be reverted with Username”
【发布时间】:2022-06-23 20:05:58
【问题描述】:

当我在下面的智能合约上运行 npx hardhat test 时,我得到了

AssertionError: Expected transaction to be reverted with Username is take please try another, but other exception was throwed: Error: VM Exception while processing transaction: reverted with reason string 'Username is taken, please try another.'

错误

这是我的solidity合约

// 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")
  });
});

这是我得到的错误

【问题讨论】:

    标签: javascript blockchain ethereum solidity smartcontracts


    【解决方案1】:

    你不见了。在期望语句中的一个之后

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-31
      • 2021-04-03
      • 2021-07-04
      • 2020-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多