简体   繁体   中英

I am getting "AssertionError: Expected transaction to be reverted with Username " when I am testing my solidity code

When I am running npx hardhat test on the below smartcontract I am getting

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

error

This is my solidity contract

// 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]];
    }
}

This is my test code(test.js file)

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

And this is the error I am getting在此处输入图像描述

You are missing . after one in the expect statement

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM