Created
September 18, 2019 15:43
-
-
Save sasoiliev/1f1567c9cedc4e4dad6f2e3739296155 to your computer and use it in GitHub Desktop.
Godog step definition re-use issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Feature: Contact us | |
Scenario: Send a message through the contact form | |
Given a user is logged in with email "user-1@example.com" | |
When the user sends a message "message-1" through the contact form | |
Then the message appears on our message board |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/DATA-DOG/godog" | |
) | |
// Production code | |
var ( | |
users = map[string]struct { | |
token string | |
}{ | |
"user-1@example.com": {token: "token-1"}, | |
"user-2@example.com": {token: "token-2"}, | |
} | |
) | |
func logIn(email string) (string, error) { | |
user, found := users[email] | |
if !found { | |
return "", fmt.Errorf("unknown user: %s", email) | |
} | |
return user.token, nil | |
} | |
// Test code below | |
type contactUsContext struct { | |
token string | |
} | |
type manageProfileContext struct { | |
token string | |
} | |
func aUserIsLoggedInWithRoleAndEmail(email string) (string, error) { | |
token, err := logIn(email) | |
if err != nil { | |
return "", err | |
} | |
return token, nil | |
} | |
func (c *contactUsContext) aUserIsLoggedInWithRoleAndEmail(email string) error { | |
var err error | |
c.token, err = aUserIsLoggedInWithRoleAndEmail(email) | |
return err | |
} | |
func (c *manageProfileContext) aUserIsLoggedInWithRoleAndEmail(email string) error { | |
var err error | |
c.token, err = aUserIsLoggedInWithRoleAndEmail(email) | |
return err | |
} | |
func (c *contactUsContext) theUserSendsAMessageThroughTheContactForm(msg string) error { | |
fmt.Printf("Contact Us token:%s\n", c.token) | |
return godog.ErrPending | |
} | |
func (c *contactUsContext) theMessageAppearsOnOurMessageBoard() error { | |
return godog.ErrPending | |
} | |
func (c *manageProfileContext) heUpdatesHisProfile() error { | |
fmt.Printf("Manage Profile token:%s\n", c.token) | |
return godog.ErrPending | |
} | |
func (c *manageProfileContext) theUpdatesAreSuccessfullyPersisted() error { | |
return godog.ErrPending | |
} | |
func SuiteContext(s *godog.Suite) { | |
contactUsCtx := contactUsContext{} | |
manageProfileCtx := manageProfileContext{} | |
s.Step(`^a user is logged in with email "([^"]*)"$`, contactUsCtx.aUserIsLoggedInWithRoleAndEmail) | |
s.Step(`^a user is logged in with email "([^"]*)"$`, manageProfileCtx.aUserIsLoggedInWithRoleAndEmail) | |
s.Step(`^the user sends a message "([^"]*)" through the contact form$`, contactUsCtx.theUserSendsAMessageThroughTheContactForm) | |
s.Step(`^the message appears on our message board$`, contactUsCtx.theMessageAppearsOnOurMessageBoard) | |
s.Step(`^he updates his profile$`, manageProfileCtx.heUpdatesHisProfile) | |
s.Step(`^the updates are successfully persisted$`, manageProfileCtx.theUpdatesAreSuccessfullyPersisted) | |
} | |
func main() { | |
godog.Run(".", func(s *godog.Suite) { | |
SuiteContext(s) | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Feature: Manage profile | |
Scenario: Update profile | |
Given a user is logged in with email "user-2@example.com" | |
When he updates his profile | |
Then the updates are successfully persisted |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment