ethers.js/docs/v4/cookbook-testing.html

308 lines
14 KiB
HTML
Raw Normal View History

2020-06-11 23:29:05 +03:00
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Testing &mdash; ethers.js 4.0.0 documentation</title>
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="_static/css/ethers.css" type="text/css" />
<link rel="stylesheet" href="_static/css/ethers.css" type="text/css" />
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="React Native" href="cookbook-react.html" />
<link rel="prev" title="Signing Messages" href="cookbook-signing.html" />
<script src="_static/js/modernizr.min.js"></script>
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search">
<a href="index.html" class="icon icon-home"> ethers.js
</a>
<div class="version">
4.0
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<p class="caption"><span class="caption-text">Developer Documentation</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="getting-started.html">Getting Started</a></li>
<li class="toctree-l1"><a class="reference internal" href="api.html">Application Programming Interface (API)</a></li>
<li class="toctree-l1"><a class="reference internal" href="api-advanced.html">Low-Level API</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="cookbook.html">Cookbook</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="cookbook-accounts.html">Accounts</a></li>
<li class="toctree-l2"><a class="reference internal" href="cookbook-contracts.html">Contracts</a></li>
<li class="toctree-l2"><a class="reference internal" href="cookbook-providers.html">Providers</a></li>
<li class="toctree-l2"><a class="reference internal" href="cookbook-signing.html">Signing Messages</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Testing</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#contract-events">Contract Events</a></li>
<li class="toctree-l3"><a class="reference internal" href="#using-multiple-accounts">Using Multiple Accounts</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="cookbook-react.html">React Native</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="migration.html">Migration Guides</a></li>
<li class="toctree-l1"><a class="reference internal" href="notes.html">Notes</a></li>
<li class="toctree-l1"><a class="reference internal" href="testing.html">Testing</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="index.html">ethers.js</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="index.html">Docs</a> &raquo;</li>
<li><a href="cookbook.html">Cookbook</a> &raquo;</li>
<li>Testing</li>
<li class="wy-breadcrumbs-aside">
<a href="_sources/cookbook-testing.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="testing">
<h1>Testing<a class="headerlink" href="#testing" title="Permalink to this headline"></a></h1>
<p>Some quick examples for techniques for testing.</p>
<hr class="docutils" />
<div class="section" id="contract-events">
<h2>Contract Events<a class="headerlink" href="#contract-events" title="Permalink to this headline"></a></h2>
<div class="literal-block-wrapper docutils container" id="id1">
<div class="code-block-caption"><span class="caption-text"><em>Testing ERC-20 Transfer Event</em></span><a class="headerlink" href="#id1" title="Permalink to this code"></a></div>
<div class="highlight-javascript notranslate"><div class="highlight"><pre><span></span><span class="nx">describe</span><span class="p">(</span><span class="s1">&#39;Events&#39;</span><span class="p">,</span> <span class="nx">async</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="nx">it</span><span class="p">(</span><span class="s1">&#39;triggers a Transfer event&#39;</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="kd">let</span> <span class="nx">contract</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Contract</span><span class="p">(</span><span class="nx">contractAddress</span><span class="p">,</span> <span class="nx">abi</span><span class="p">,</span> <span class="nx">accounts</span><span class="p">[</span><span class="mi">0</span><span class="p">]);</span>
<span class="kd">let</span> <span class="nx">transferEvent</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">Promise</span><span class="p">((</span><span class="nx">resolve</span><span class="p">,</span> <span class="nx">reject</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="p">{</span>
<span class="nx">contract</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">&#39;Transfer&#39;</span><span class="p">,</span> <span class="p">(</span><span class="nx">from</span><span class="p">,</span> <span class="nx">to</span><span class="p">,</span> <span class="nx">amount</span><span class="p">,</span> <span class="nx">event</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="p">{</span>
<span class="nx">event</span><span class="p">.</span><span class="nx">removeListener</span><span class="p">();</span>
<span class="nx">resolve</span><span class="p">({</span>
<span class="nx">from</span><span class="o">:</span> <span class="nx">from</span><span class="p">,</span>
<span class="nx">to</span><span class="o">:</span> <span class="nx">to</span><span class="p">,</span>
<span class="nx">amount</span><span class="o">:</span> <span class="nx">amount</span>
<span class="p">});</span>
<span class="p">});</span>
<span class="nx">setTimeout</span><span class="p">(()</span> <span class="p">=&gt;</span> <span class="p">{</span>
<span class="nx">reject</span><span class="p">(</span><span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s1">&#39;timeout&#39;</span><span class="p">));</span>
<span class="p">},</span> <span class="mi">60000</span><span class="p">)</span>
<span class="p">});</span>
<span class="kd">let</span> <span class="nx">tx</span> <span class="o">=</span> <span class="nx">await</span> <span class="nx">contract</span><span class="p">.</span><span class="nx">transfer</span><span class="p">(</span><span class="nx">accounts</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="mi">12345</span><span class="p">);</span>
<span class="kd">let</span> <span class="nx">event</span> <span class="o">=</span> <span class="nx">await</span> <span class="nx">transferEvent</span><span class="p">;</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">equal</span><span class="p">(</span><span class="nx">event</span><span class="p">.</span><span class="nx">from</span><span class="p">,</span> <span class="nx">account</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">address</span><span class="p">);</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">equal</span><span class="p">(</span><span class="nx">event</span><span class="p">.</span><span class="nx">to</span><span class="p">,</span> <span class="nx">account</span><span class="p">[</span><span class="mi">1</span><span class="p">].</span><span class="nx">address</span><span class="p">);</span>
<span class="nx">assert</span><span class="p">.</span><span class="nx">equal</span><span class="p">(</span><span class="nx">event</span><span class="p">.</span><span class="nx">amount</span><span class="p">.</span><span class="nx">toNumber</span><span class="p">(),</span> <span class="mi">12345</span><span class="p">);</span>
<span class="p">});</span>
<span class="p">});</span>
</pre></div>
</div>
</div>
</div>
<hr class="docutils" />
<div class="section" id="using-multiple-accounts">
<h2>Using Multiple Accounts<a class="headerlink" href="#using-multiple-accounts" title="Permalink to this headline"></a></h2>
<div class="literal-block-wrapper docutils container" id="id2">
<div class="code-block-caption"><span class="caption-text"><em>Testing Multiple Accounts</em></span><a class="headerlink" href="#id2" title="Permalink to this code"></a></div>
<div class="highlight-javascript notranslate"><div class="highlight"><pre><span></span><span class="nx">describe</span><span class="p">(</span><span class="s1">&#39;Events&#39;</span><span class="p">,</span> <span class="nx">async</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="nx">it</span><span class="p">(</span><span class="s1">&#39;triggers a Transfer event&#39;</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="c1">// Connect to Geth/Parity node on http://localhost:8545</span>
<span class="kd">let</span> <span class="nx">provider</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">providers</span><span class="p">.</span><span class="nx">JsonRpcProvider</span><span class="p">();</span>
<span class="c1">// Get the first two accounts from the Geth/Parity node</span>
<span class="kd">let</span> <span class="nx">signer0</span> <span class="o">=</span> <span class="nx">provider</span><span class="p">.</span><span class="nx">getSigner</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<span class="kd">let</span> <span class="nx">signer1</span> <span class="o">=</span> <span class="nx">provider</span><span class="p">.</span><span class="nx">getSigner</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
<span class="c1">// Read-only connection to the contract</span>
<span class="kd">let</span> <span class="nx">contract</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Contract</span><span class="p">(</span><span class="nx">contractAddress</span><span class="p">,</span> <span class="nx">abi</span><span class="p">,</span> <span class="nx">provider</span><span class="p">);</span>
<span class="c1">// Read-Write connection to the contract</span>
<span class="kd">let</span> <span class="nx">contractAsSigner0</span> <span class="o">=</span> <span class="nx">contract</span><span class="p">.</span><span class="nx">connect</span><span class="p">(</span><span class="nx">signer0</span><span class="p">);</span>
<span class="kd">let</span> <span class="nx">contractAsSigner1</span> <span class="o">=</span> <span class="nx">contract</span><span class="p">.</span><span class="nx">connect</span><span class="p">(</span><span class="nx">signer1</span><span class="p">);</span>
<span class="c1">// ...</span>
<span class="p">});</span>
<span class="p">});</span>
</pre></div>
</div>
</div>
<hr class="docutils" />
</div>
</div>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="cookbook-react.html" class="btn btn-neutral float-right" title="React Native" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="cookbook-signing.html" class="btn btn-neutral" title="Signing Messages" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
&copy; Copyright 2016, Richard Moore &lt;me@ricmoo.com&gt;
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="_static/language_data.js"></script>
<script type="text/javascript" src="_static/js/theme.js"></script>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>