Skip to content

Instantly share code, notes, and snippets.

@zeddee
Forked from natemurthy/helloworld_test.go
Created December 19, 2018 09:11
Show Gist options
  • Save zeddee/6e317f8271a9c8f6cc5f4b00b0b7b237 to your computer and use it in GitHub Desktop.
Save zeddee/6e317f8271a9c8f6cc5f4b00b0b7b237 to your computer and use it in GitHub Desktop.
package main
import (
"math/big"
"testing"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/suite"
)
type TestSuite struct {
suite.Suite // Designates this struct as a test suite
auth *bind.TransactOpts
address common.Address
gAlloc core.GenesisAlloc
sim *backends.SimulatedBackend
instance *Helloworld // Instance of our contract as a go object
}
func TestRunHelloworldSuite(t *testing.T) {
suite.Run(t, new(TestSuite))
}
func (s *TestSuite) SetupTest() {
// We don't need a Wallet/account specific key, just a general ecdsa one.
key, _ := crypto.GenerateKey()
// Create new keyed transactor
s.auth = bind.NewKeyedTransactor(key)
s.address = s.auth.From
s.gAlloc = map[common.Address]core.GenesisAccount{
s.address: {Balance: big.NewInt(10000000000)},
}
s.sim = backends.NewSimulatedBackend(s.gAlloc)
ver := "1.0"
// Run the Deploy<contract_name>(auth *bind.TransactOpts, backend bind.ContractBackend, _version string)
// Returns a contractAddress common.Address, tx *types.Transaction
// (which we discard because we don't need to locate this on our simulated blockchain)
// a contract instance, and an error, which we check with Testify using s.Nil(err)
_, _, instance, err := DerrployHelloworld(s.auth, s.sim, ver)
// We assign the contract instance returned to s.instance so we can interact with it
// with the rest of our tests
s.instance = instance
s.Nil(err)
// Creates a block on our simulated blockchain
s.sim.Commit()
}
// TestSay tests the Say() contract function
func (s *TestSuite) TestSay() {
str, err := s.instance.Say(nil)
s.Equal("hello etherworld", str)
s.Nil(err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment