我的环境是:Python3.9.9 Pytest 6.2.5 Brownie 1.17.1
我正在Youtube上编写Patrick的智能合同教程中的test_fund_me.py;在这个阶段,我应该运行测试,并通过Pytest包含一个异常,这样只有合同的所有者才能调用该函数。我添加了pytest.raises(exceptions.VirtualMachineError)方法,但它仍然返回一个失败的测试,并引发标题中提到的错误。
这是我的代码:
from scripts.helpful_scripts import get_account, LOCAL_BLOCKCHAIN_ENVIRONMENTS
from scripts.deploy import deploy_fund_me
from brownie import network, accounts, exceptions
import pytest
def test_can_fund_and_withdraw():
account = get_account()
fund_me = deploy_fund_me()
entrance_fee = fund_me.getEntranceFee() + 100
tx = fund_me.fund({"from": account, "value": entrance_fee})
tx.wait(1)
assert fund_me.addressToAmountFunded(account.address) == entrance_fee
tx2 = fund_me.withdraw({"from": account})
tx2.wait(1)
assert fund_me.addressToAmountFunded(account.address) == 0
def test_only_owner_can_withdraw():
if network.show_active() not in LOCAL_BLOCKCHAIN_ENVIRONMENTS:
pytest.skip("only for local testing")
fund_me = deploy_fund_me()
bad_actor = accounts.add()
with pytest.raises(exceptions.VirtualMachineError):
fund_me.withdraw({"from": bad_actor})下面是错误消息:
PS C:\Users\chret\Documents\demo\brownie_fund_me> brownie test -k test_only_owner_can_withdraw
INFO: Could not find files for the given pattern(s).
Brownie v1.17.1 - Python development framework for Ethereum
=============================================================================== test session starts ================================================================================
platform win32 -- Python 3.9.9, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: C:\Users\chret\Documents\demo\brownie_fund_me
plugins: eth-brownie-1.17.1, hypothesis-6.24.0, forked-1.3.0, xdist-1.34.0, web3-5.24.0
collected 2 items / 1 deselected / 1 selected
Launching 'ganache-cli.cmd --accounts 10 --hardfork istanbul --gasLimit 12000000 --mnemonic brownie --port 8545'...
tests\test_fund_me.py F [100%]
===================================================================================== FAILURES =====================================================================================
___________________________________________________________________________ test_only_owner_can_withdraw ___________________________________________________________________________
def test_only_owner_can_withdraw():
if network.show_active() not in LOCAL_BLOCKCHAIN_ENVIRONMENTS:
pytest.skip("only for local testing")
fund_me = deploy_fund_me()
bad_actor = accounts.add()
with pytest.raises(exceptions.VirtualMachineError):
> fund_me.withdraw({"from": bad_actor})
tests\test_fund_me.py:25:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\..\..\AppData\Local\Programs\Python\Python39\lib\site-packages\brownie\network\contract.py:1625: in __call__
return self.transact(*args)
..\..\..\AppData\Local\Programs\Python\Python39\lib\site-packages\brownie\network\contract.py:1498: in transact
return tx["from"].transfer(
..\..\..\AppData\Local\Programs\Python\Python39\lib\site-packages\brownie\network\account.py:690: in transfer
receipt._raise_if_reverted(exc)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <Transaction '0xb66de8420866ddd8efba108a2b401d80a64cbdeb780ea09ef75a73185809bbca'>, exc = None
def _raise_if_reverted(self, exc: Any) -> None:
if self.status or CONFIG.mode == "console":
return
if not web3.supports_traces:
# if traces are not available, do not attempt to determine the revert reason
raise exc or ValueError("Execution reverted")
if self._dev_revert_msg is None:
# no revert message and unable to check dev string - have to get trace
self._expand_trace()
if self.contract_address:
source = ""
elif CONFIG.argv["revert"]:
source = self._traceback_string()
else:
source = self._error_string(1)
contract = state._find_contract(self.receiver)
if contract:
marker = "//" if contract._build["language"] == "Solidity" else "#"
line = self._traceback_string().split("\n")[-1]
if marker + " dev: " in line:
self._dev_revert_msg = line[line.index(marker) + len(marker) : -5].strip()
> raise exc._with_attr(
source=source, revert_msg=self._revert_msg, dev_revert_msg=self._dev_revert_msg
)
E AttributeError: 'NoneType' object has no attribute '_with_attr'
..\..\..\AppData\Local\Programs\Python\Python39\lib\site-packages\brownie\network\transaction.py:446: AttributeError
------------------------------------------------------------------------------- Captured stdout call -------------------------------------------------------------------------------
The active network is development
Deploying Mocks...
Mocks Deployed!
Contract deployed to 0x602C71e4DAC47a042Ee7f46E0aee17F94A3bA0B6
mnemonic: 'veteran company dinosaur actual jump club quit horn walk gym jar melody'
============================================================================= short test summary info ==============================================================================
FAILED tests/test_fund_me.py::test_only_owner_can_withdraw - AttributeError: 'NoneType' object has no attribute '_with_attr'
========================================================================= 1 failed, 1 deselected in 4.70s ==========================================================================
Terminating local RPC client...
PS C:\Users\chret\Documents\demo\brownie_fund_me>我已经查找了这个"NoneType“错误是什么,据我所知,它可能来自对一个帐户的bad_actor调用,看起来bad_actor = account.add()返回一种None类型。但我不确定,我也不知道怎么解决这个问题。
几天前,我看到有人在同一件事上打开了一个问题,关闭了终点站,这个问题就消失了。这不是我的情况,我甚至重新启动了我的电脑,问题依然存在。
(感谢任何帮助:)
发布于 2021-12-29 19:16:19
我也遇到了同样的问题,如果我更改了传递给pytest.raises()的内容,我就可以让它工作起来:
使用pytest.raises(AttributeError):
发布于 2022-03-20 23:02:41
这是一个bug,修正了Browniev1.18.1。使用以下命令升级brownie:
pipx upgrade eth-brownie使用Python 3.9.11在我的环境中工作
参考文献:https://github.com/eth-brownie/brownie/issues/1434#issue-1129651456
发布于 2021-12-23 09:40:07
尝试从布朗尼中获取最新的FundMe
from brownie import FundMe, network
from scripts.helpful_scripts import get_account, LOCAL_BLOCKCHAIN_ENVIRONMENTS
from scripts.deploy import deploy_fund_me
from brownie import network, accounts, exceptions
import pytest
def test_can_fund_and_withdraw():
account = get_account()
deploy_fund_me()
fund_me = FundMe[-1]
entrance_fee = fund_me.getEntranceFee()
tx = fund_me.fund({"from": account, "value": entrance_fee})
tx.wait(1)
assert fund_me.addressToAmountFunded(account.address) == entrance_fee
tx2 = fund_me.withdraw({"from": account})
tx2.wait(1)
assert fund_me.addressToAmountFunded(account.address) == 0
def test_only_owner_can_withdraw():
if network.show_active() not in LOCAL_BLOCKCHAIN_ENVIRONMENTS:
pytest.skip("only for local testing")
deploy_fund_me()
fund_me = FundMe[-1]
bad_actor = accounts.add()
with pytest.raises(exceptions.VirtualMachineError):
fund_me.withdraw({"from": bad_actor})https://stackoverflow.com/questions/70242806
复制相似问题