簡體   English   中英

brownie - 我如何處理異常?

[英]brownie - How do I handle exceptions?

我一直在看帕特里克(很棒的小伙子)的布朗尼教程視頻。 不過,在運行特定的 function 時,我似乎遇到了錯誤,但功能的 rest 運行完美。 我不知道我哪里錯了。

我附上了代碼片段和錯誤消息

def test_can_pick_winner_correctly():
        # Arrange
        if network.show_active() not in LOCAL_BLOCKCHAIN_ENVIRONMENTS:
            pytest.skip()
        lottery = deploy_lottery()
        account = get_account()
        lottery.startLottery({"from": account})
        lottery.enter({"from": account, "value": lottery.getEntranceFee()})
        lottery.enter({"from": get_account(index=1), "value": lottery.getEntranceFee()})
        lottery.enter({"from": get_account(index=2), "value": lottery.getEntranceFee()})
        fund_with_link(lottery)
        starting_balance_of_account = account.balance()
        balance_of_lottery = lottery.balance()
        transaction = lottery.endLottery({"from": account})
>       request_id = transaction.events["RequestedRandomness"]["requestId"]
E       brownie.exceptions.EventLookupError: Event 'RequestedRandomness' did not fire.

tests/test_lottery_uint.py:78: EventLookupError
------------------------------------------------------- Captured stdout call --------------------------------------------------------
Deployed lottery!
Fund contract!
====================================================== short test summary info ======================================================
FAILED tests/test_lottery_uint.py::test_can_pick_winner_correctly - brownie.exceptions.EventLookupError: Event 'RequestedRandomnes...

這是錯誤消息,下面是包含導入和特定 function 的代碼片段

    from scripts.helpful_scripts import (
        LOCAL_BLOCKCHAIN_ENVIRONMENTS,
        get_account,
        fund_with_link,
        get_contract,
    )
    from brownie import Lottery, accounts, config, network, exceptions
    from scripts.deploy_lottery import deploy_lottery
    from web3 import Web3
    import pytest

這些是進口的,下面是特定的 function

 def test_can_pick_winner_correctly():
        # Arrange
        if network.show_active() not in LOCAL_BLOCKCHAIN_ENVIRONMENTS:
            pytest.skip()
        lottery = deploy_lottery()
        account = get_account()
        lottery.startLottery({"from": account})
        lottery.enter({"from": account, "value": lottery.getEntranceFee()})
        lottery.enter({"from": get_account(index=1), "value": lottery.getEntranceFee()})
        lottery.enter({"from": get_account(index=2), "value": lottery.getEntranceFee()})
        fund_with_link(lottery)
        starting_balance_of_account = account.balance()
        balance_of_lottery = lottery.balance()
        transaction = lottery.endLottery({"from": account})
        request_id = transaction.events["RequestedRandomness"]["requestId"]
        STATIC_RNG = 777
        get_contract("vrf_coordinator").callBackWithRandomness(
            request_id, STATIC_RNG, lottery.address, {"from": account}
        )
        # 777 % 3 = 0
        assert lottery.recentWinner() == account
        assert lottery.balance() == 0
        assert account.balance() == starting_balance_of_account + balance_of_lottery'

您確定要從 Share the code from 發出該事件嗎

Lottery.sol:endLottery 方法? 在復制粘貼您提供的測試方法后,它成功通過。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM