Skip to content

Instantly share code, notes, and snippets.

@coop
Created April 30, 2012 10:17
Show Gist options
  • Save coop/2557027 to your computer and use it in GitHub Desktop.
Save coop/2557027 to your computer and use it in GitHub Desktop.
require 'test_helper'
class PayPalExpress::TransactionSearchTest < ActiveSupport::TestCase
test "#search returns a collection of transactions" do
account = MiniTest::Mock.new
period = MiniTest::Mock.new
requester = MiniTest::Mock.new
params = {}
params['ACK'] = 'Success'
params.default = []
requester.expect :perform, params
transaction_search = PayPalExpress::TransactionSearch.new :period => period, :account => account, :requester => requester
transactions = transaction_search.search
assert_empty transactions
requester.verify
end
test "#search sends an email when PayPal returns a failure" do
account = MiniTest::Mock.new
period = MiniTest::Mock.new
requester = MiniTest::Mock.new
notifier = MiniTest::Mock.new
notifier.expect :deliver
params = {}
params['ACK'] = 'Error'
params.default = []
transaction_search = PayPalExpress::TransactionSearch.new :period => period, :account => account, :requester => requester
transactions = transaction_search.search
assert notifier.verify
end
end
@coop
Copy link
Author

coop commented Apr 30, 2012

I am having to build a lot of setup here to test and I feel that what I'm actually wanting to test is getting lost. I think it might be more intention revealing if I ditch the hash (which I probably should be doing anyway) and do something more like:

requester = MiniTest::Mock.new
requester.expect :success?, false

Even writing that out feels better - at the moment I'm exposing too much of the implementation.

@coop
Copy link
Author

coop commented Apr 30, 2012

I ended up doing something very similar to my proposal.

require 'test_helper'

class PayPalExpress::TransactionSearchTest < ActiveSupport::TestCase
  test "#search returns a collection of transactions" do
    account   = MiniTest::Mock.new
    period    = MiniTest::Mock.new
    requester = MiniTest::Mock.new
    requester.expect :perform, OpenStruct.new(:success? => true)

    transaction_search = PayPalExpress::TransactionSearch.new :period => period, :account => account, :requester => requester
    transactions       = transaction_search.search

    assert_empty transactions
    assert requester.verify
  end

  test "#search sends an email when PayPal returns a failure" do
    account   = MiniTest::Mock.new
    period    = MiniTest::Mock.new
    requester = MiniTest::Mock.new
    requester.expect :perform, OpenStruct.new(:success? => true)
    notifier  = MiniTest::Mock.new
    notifier.expect :deliver_failed_transaction_search

    transaction_search = PayPalExpress::TransactionSearch.new :period => period, :account => account, :requester => requester
    transactions       = transaction_search.search

    assert requester.verify
    assert notifier.verify
  end
end

I wrapped the response object in code land because it's gross and could change. This allows me to not depend on the hash structure that I was relying on and instead rely on message passing!

@tatey
Copy link

tatey commented Apr 30, 2012

Looks good, bro.

@davekiss
Copy link

Not sure how I came across this, but dig the badge.

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