Skip to content

Instantly share code, notes, and snippets.

@zer0tonin
Created March 9, 2019 17:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zer0tonin/c5637ed53691ec3245518035b9799afc to your computer and use it in GitHub Desktop.
Save zer0tonin/c5637ed53691ec3245518035b9799afc to your computer and use it in GitHub Desktop.
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
Copy link

AlbertMorenoDEV commented Mar 29, 2019

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

@davidaparicio
Copy link

My version with negative values & the latest GoDog version: https://gist.github.com/davidaparicio/9fa9e664b6eed47e054ee6de426abe1a :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment