简体   繁体   English

当我测试我的可靠性代码时,我收到“AssertionError: Expected transaction to be reverted with Username”

[英]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当我在下面的智能合约上运行 npx npx hardhat test时,我得到了

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.' AssertionError:使用用户名恢复的预期事务请尝试另一个,但引发了其他异常:错误:处理事务时出现VM异常:使用原因字符串“用户名被采用,请尝试另一个”。

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)这是我的测试代码(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")
  });
});

And this is the error I am getting这是我得到的错误在此处输入图像描述

You are missing .你失踪了。 after one in the expect statement在期望语句中的一个之后

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript:为什么我得到这个AssertionError? - JavaScript: Why am I getting this AssertionError? 重构jQuery代码时,为什么会得到[object Object]而不是预期的HTML? - Why am I getting [object Object] instead of my expected HTML when refactoring jQuery code? 为什么我在邮递员的架构中收到此错误:架构有效 | 断言错误:未知属性(不在架构中):预期假等于真 - Why am I getting this error in my schema in postman: Schema is valid | AssertionError: Unknown property (not in schema): expected false to equal true 为什么在尝试运行我的代码时会收到 TypeError? - Why am I getting a TypeError when trying to run my code? 为什么在运行我的代码时会收到错误“UnhandledPromiseRejectionWarning”? - Why am I getting error "UnhandledPromiseRejectionWarning" when running my code? 为什么我的代码中出现错误 - Why am I getting a error in my code 当我将 Svelte-grid 用于 React 时,为什么在 VS Code 中会出现“预期标识符” - Why am I getting “Identifier expected” in VS Code when I use Svelte-grid for React 我做错了什么,因为我的代码没有给出预期的结果 - what am i doing wrong as my code is not giving expected results 嗨,我有一个 Solidity 初学者问题:我正在尝试部署带有已签名交易的合同 - Hi, I have a Solidity beginner question: I am trying to deploy a contract with signed transaction 在运行使用solidity 智能合约的next.js 应用程序时,出现“无法读取未定义的属性”错误 - While running my next.js app that is using solidity smart contracts, I am getting "Cannot read properties of undefined" error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM