Skip to content

Commit

Permalink
feat(pactwritemode): allow user to set pact write mode.Fixes #30
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed May 12, 2017
1 parent 2550447 commit 9f2c7fe
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,26 @@ publishing or retrieving Pact files to/from a Pact Broker:
* `BrokerUsername` - the username for Pact Broker basic authentication.
* `BrokerPassword` - the password for Pact Broker basic authentication.

### Splitting tests across multiple files

Pact tests tend to be quite long, due to the need to be specific about request/response payloads. Often times it is nicer to be able to split your tests across multiple files for manageability.

You have two options to achieve this feat:

1. Set `PactFileWriteMode` to `"update"` when creating a `Pact` struct:

This will allow you to have multiple independent tests for a given Consumer-Provider pair, without it clobbering previous interactions.

See this [PR](https://github.com/pact-foundation/pact-js/pull/48) for background.

_NOTE_: If using this approach, you *must* be careful to clear out existing pact files (e.g. `rm ./pacts/*.json`) before you run tests to ensure you don't have left over requests that are no longer relevent.

1. Create a Pact test helper to orchestrate the setup and teardown of the mock service for multiple tests.

In larger test bases, this can reduce test suite time and the amount of code you have to manage.

See the JS [example](https://github.com/tarciosaraiva/pact-melbjs/blob/master/helper.js) and related [issue](https://github.com/pact-foundation/pact-js/issues/11) for more.

### Output Logging

Pact Go uses a simple log utility ([logutils](https://github.com/hashicorp/logutils))
Expand Down
16 changes: 15 additions & 1 deletion dsl/mock_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ type MockService struct {

// Provider name.
Provider string

// PactFileWriteMode specifies how to write to the Pact file, for the life
// of a Mock Service.
// "overwrite" will always truncate and replace the pact after each run
// "update" will append to the pact file, which is useful if your tests
// are split over multiple files and instantiations of a Mock Server
// See https://github.com/realestate-com-au/pact/blob/master/documentation/configuration.md#pactfile_write_mode
PactFileWriteMode string
}

// call sends a message to the Pact service
Expand Down Expand Up @@ -84,16 +92,22 @@ func (m *MockService) Verify() error {
// WritePact writes the pact file to disk.
func (m *MockService) WritePact() error {
log.Println("[DEBUG] mock service write pact")

if m.Consumer == "" || m.Provider == "" {
return errors.New("Consumer and Provider name need to be provided")
}
pact := map[string]map[string]string{
if m.PactFileWriteMode == "" {
m.PactFileWriteMode = "overwrite"
}

pact := map[string]interface{}{
"consumer": map[string]string{
"name": m.Consumer,
},
"provider": map[string]string{
"name": m.Provider,
},
"pactFileWriteMode": m.PactFileWriteMode,
}

url := fmt.Sprintf("%s/pact", m.BaseURL)
Expand Down
15 changes: 12 additions & 3 deletions dsl/pact.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ type Pact struct {
// Defaults to `<cwd>/pacts`.
PactDir string

// PactFileWriteMode specifies how to write to the Pact file, for the life
// of a Mock Service.
// "overwrite" will always truncate and replace the pact after each run
// "update" will append to the pact file, which is useful if your tests
// are split over multiple files and instantiations of a Mock Server
// See https://github.com/realestate-com-au/pact/blob/master/documentation/configuration.md#pactfile_write_mode
PactFileWriteMode string

// Specify which version of the Pact Specification should be used (1 or 2).
// Defaults to 2.
SpecificationVersion int
Expand Down Expand Up @@ -192,9 +200,10 @@ func (p *Pact) WritePact() error {
p.Setup(true)
log.Printf("[DEBUG] pact write Pact file")
mockServer := MockService{
BaseURL: fmt.Sprintf("http://%s:%d", p.Host, p.Server.Port),
Consumer: p.Consumer,
Provider: p.Provider,
BaseURL: fmt.Sprintf("http://%s:%d", p.Host, p.Server.Port),
Consumer: p.Consumer,
Provider: p.Provider,
PactFileWriteMode: p.PactFileWriteMode,
}
err := mockServer.WritePact()
if err != nil {
Expand Down

0 comments on commit 9f2c7fe

Please sign in to comment.