Skip to content

Update README.md #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Given we have a client that needs to make a HTTP GET request to a sinatra webapp

client.rb:

```ruby
require 'httparty'
require 'uri'
require 'json'
Expand All @@ -33,10 +34,12 @@ client.rb:


end
```

and the producer:
producer.rb

```ruby
require 'sinatra/base'
require 'json'

Expand All @@ -54,33 +57,37 @@ producer.rb
end

end

```

This producer expects a valid_date parameter in HTTP date format, and then returns some simple json back.

Running the client with the following rake task against the producer works nicely:

```ruby
desc 'Run the client'
task :run_client => :init do
require 'client'
require 'ap'
ap Client.new.load_producer_json
end

```

$ rake run_client

```ruby
http://localhost:8081/producer.json?valid_date=Thu,%2015%20Aug%202013%2003:15:15%20GMT
{
"test" => "NO",
"valid_date" => "2013-08-15T13:31:39+10:00",
"count" => 1000
}

```

Now lets get the client to use the data it gets back from the producer. Here is the updated client method that uses the returned data:

client.rb

```ruby
def process_data
data = load_producer_json
ap data
Expand All @@ -90,11 +97,13 @@ client.rb
puts date
[value, date]
end
```

Add a spec to test this client:

client_spec.rb:

```ruby
require 'spec_helper'
require 'client'

Expand All @@ -119,6 +128,7 @@ client_spec.rb:


end
```

Let's run this spec and see it all pass:

Expand Down Expand Up @@ -149,6 +159,8 @@ Lets setup Pact in the consumer. Pact lets the consumers define the expectations

spec_helper.rb:


```ruby
require 'ap'

require 'pact'
Expand All @@ -170,14 +182,15 @@ spec_helper.rb:
port 8081
end
end

```

This defines a consumer and a producer that runs on port 8081.

The spec for the client now has a pact section.

client_spec.rb:

```ruby
describe 'pact with producer', :pact => true do

let(:date) { Time.now.httpdate }
Expand All @@ -203,7 +216,7 @@ client_spec.rb:
end

end

```

Running this spec still passes, but it creates a pact file which we can use to validate our assumptions on the producer side.

Expand Down Expand Up @@ -271,25 +284,25 @@ Generated pact file (spec/pacts/my_consumer-my_producer.json):
}
}
}

#Producer Setup#

Pact has a rake task to verify the producer against the generated pact file. It can get the pact file from any URL (like the last sucessful CI build), but we just going to use the local one. Here is the addition to the Rakefile.

Rakefile:

```ruby
require 'pact'
require 'pact/verification_task'


Pact::VerificationTask.new(:local) do | pact |
pact.uri 'spec/pacts/my_consumer-my_producer.json', support_file: './spec/pacts/pact_helper'
end
```

The pact_helper needs to tell Pact which Rack app it needs to test against.

spec/pacts/pact_helper.rb

```ruby
Pact.with_consumer 'My Consumer' do
producer_state "producer is in a sane state" do
set_up do
Expand All @@ -306,12 +319,11 @@ spec/pacts/pact_helper.rb
app { Producer.new }
end
end

```
Now if we run our pact verification task, it should fail.

$ rake pact:verify:local


Pact in spec/pacts/my_consumer-my_producer.json
Given producer is in a sane state
a request for producer json to /producer.json
Expand Down