Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The art of Mocking

The art of Mocking

What mocks are, how they fit the OOP world and how to use them in Ruby.

Federico Ravasio

November 20, 2014
Tweet

More Decks by Federico Ravasio

Other Decks in Programming

Transcript

  1. 1. How mocks fit in the OOP world 2. Mock

    objects in practice 3. Rules on how to use them 4. Common misconceptions
  2. class ApiClient # @http_client from the constructor def is_it_up? @http_client.head("...")

    true rescue NetworkError false end end ApiClient.new(HTTPClient.new).is_it_up? # => false
  3. describe ApiClient do ... describe "#is_it_up?" do context "when HEAD

    request succeeds" do before do expect(http_client) .to_receive(:head) .with("...") end it "returns true" do expect(subject.is_it_up?).to be true end end end end
  4. describe ApiClient do ... describe "#is_it_up?" do context "when HEAD

    request fails" do before do expect(http_client) .to_receive(:head) .and_raise(NetworkError) end it "returns false" do expect(subject.is_it_up?).to be false end end end end