daily pastebin goal
91%
SHARE
TWEET

Untitled

a guest Dec 19th, 2018 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "math/big"
  5.     "testing"
  6.  
  7.     "github.com/ethereum/go-ethereum/accounts/abi/bind"
  8.     "github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
  9.     "github.com/ethereum/go-ethereum/common"
  10.     "github.com/ethereum/go-ethereum/core"
  11.     "github.com/ethereum/go-ethereum/crypto"
  12.     "github.com/stretchr/testify/suite"
  13. )
  14.  
  15. type TestSuite struct {
  16.     suite.Suite // Designates this struct as a test suite
  17.     auth        *bind.TransactOpts
  18.     address     common.Address
  19.     gAlloc      core.GenesisAlloc
  20.     sim         *backends.SimulatedBackend
  21.     instance    *Helloworld // Instance of our contract as a go object
  22. }
  23.  
  24. func TestRunHelloworldSuite(t *testing.T) {
  25.     suite.Run(t, new(TestSuite))
  26. }
  27.  
  28. func (s *TestSuite) SetupTest() {
  29.     // We don't need a Wallet/account specific key, just a general ecdsa one.
  30.     key, _ := crypto.GenerateKey()
  31.  
  32.     // Create new keyed transactor
  33.     s.auth = bind.NewKeyedTransactor(key)
  34.  
  35.     s.address = s.auth.From
  36.  
  37.     s.gAlloc = map[common.Address]core.GenesisAccount{
  38.         s.address: {Balance: big.NewInt(10000000000)},
  39.     }
  40.  
  41.     s.sim = backends.NewSimulatedBackend(s.gAlloc)
  42.  
  43.     ver := "1.0"
  44.     // Run the Deploy<contract_name>(auth *bind.TransactOpts, backend bind.ContractBackend, _version string)
  45.     // Returns a contractAddress common.Address, tx *types.Transaction
  46.     // (which we discard because we don't need to locate this on our simulated blockchain)
  47.     // a contract instance, and an error, which we check with Testify using s.Nil(err)
  48.     _, _, instance, err := DerrployHelloworld(s.auth, s.sim, ver)
  49.  
  50.     // We assign the contract instance returned to s.instance so we can interact with it
  51.     // with the rest of our tests
  52.     s.instance = instance
  53.     s.Nil(err)
  54.  
  55.     // Creates a block on our simulated blockchain
  56.     s.sim.Commit()
  57. }
  58.  
  59. // TestSay tests the Say() contract function
  60. func (s *TestSuite) TestSay() {
  61.     str, err := s.instance.Say(nil)
  62.     s.Equal("hello etherworld", str)
  63.     s.Nil(err)
  64. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top