Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
BDD in go
Feature: bank account
A user's bank account must be able to withdraw and deposit cash
Scenario Outline: Deposit
Given I have a bank account with <start>$
When I deposit <deposit>$
Then it should have a balance of <end>$
Examples:
| start | deposit | end |
| 10 | 0 | 10 |
| 10 | 10 | 20 |
| 100 | 50 | 150 |
Scenario Outline: Withdrawal
Given I have a bank account with <start>$
When I withdraw <withdrawal>$
Then it should have a balance of <end>$
Examples:
| start | withdrawal | end |
| 10 | 0 | 10 |
| 20 | 10 | 10 |
| 100 | 50 | 50 |
package bank
import (
"flag"
"fmt"
"os"
"testing"
"github.com/DATA-DOG/godog"
)
var opt = godog.Options{
Format: "progress",
}
func init() {
godog.BindFlags("godog.", flag.CommandLine, &opt)
}
func TestMain(m *testing.M) {
flag.Parse()
opt.Paths = flag.Args()
status := godog.RunWithOptions("godogs", func(s *godog.Suite) {
FeatureContext(s)
}, opt)
if st := m.Run(); st > status {
status = st
}
os.Exit(status)
}
var testAccount *account
func iHaveABankAccountWith(balance int) error {
testAccount = &account{balance:balance}
return nil
}
func iDeposit(amount int) error {
testAccount.deposit(amount)
return nil
}
func iWithdraw(amount int) error {
testAccount.withdraw(amount)
return nil
}
func itShouldHaveABalanceOf(balance int) error {
if testAccount.balance == balance {
return nil
}
return fmt.Errorf("Incorrect account balance")
}
func FeatureContext(s *godog.Suite) {
s.Step(`^I have a bank account with (\d+)\$$`, iHaveABankAccountWith)
s.Step(`^I deposit (\d+)\$$`, iDeposit)
s.Step(`^I withdraw (\d+)\$$`, iWithdraw)
s.Step(`^it should have a balance of (\d+)\$$`, itShouldHaveABalanceOf)
s.BeforeScenario(func(interface{}) {
testAccount = nil
})
}
package bank
import (
"flag"
"fmt"
"os"
"testing"
"github.com/DATA-DOG/godog"
)
var opt = godog.Options{
Format: "progress",
}
func init() {
godog.BindFlags("godog.", flag.CommandLine, &opt)
}
func TestMain(m *testing.M) {
flag.Parse()
opt.Paths = flag.Args()
status := godog.RunWithOptions("godogs", func(s *godog.Suite) {
FeatureContext(s)
}, opt)
if st := m.Run(); st > status {
status = st
}
os.Exit(status)
}
var testAccount *account
func iHaveABankAccountWith(balance int) error {
testAccount = &account{balance:balance}
return nil
}
func iDeposit(amount int) error {
testAccount.deposit(amount)
return nil
}
func iWithdraw(amount int) error {
testAccount.withdraw(amount)
return nil
}
func itShouldHaveABalanceOf(balance int) error {
if testAccount.balance == balance {
return nil
}
return fmt.Errorf("Incorrect account balance")
}
func FeatureContext(s *godog.Suite) {
s.Step(`^I have a bank account with (\d+)\$$`, iHaveABankAccountWith)
s.Step(`^I deposit (\d+)\$$`, iDeposit)
s.Step(`^I withdraw (\d+)\$$`, iWithdraw)
s.Step(`^it should have a balance of (\d+)\$$`, itShouldHaveABalanceOf)
s.BeforeScenario(func(interface{}) {
testAccount = nil
})
}
module bank
require github.com/DATA-DOG/godog v0.7.10
github.com/DATA-DOG/godog v0.7.10 h1:BRaQdCOsFth//Ep/J6gtb3xOeDh+sAVjL44ZdK1E9aI=
github.com/DATA-DOG/godog v0.7.10/go.mod h1:z2OZ6a3X0/YAKVqLfVzYBwFt3j6uSt3Xrqa7XTtcQE0=
@AlbertMorenoDEV

This comment has been minimized.

Copy link

AlbertMorenoDEV commented Mar 29, 2019

Seems to me that account.go has been overwritten by account_test.go content :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.