Permalink
Browse files

Add ability to generate a transaction.

  • Loading branch information...
1 parent fab3c92 commit 4cb5cb0d30d91471ac3063786f3126fc3bbdeb36 Gabe Berke-Williams committed Nov 20, 2011
Showing with 69 additions and 0 deletions.
  1. +29 −0 README.md
  2. +8 −0 lib/fake_braintree.rb
  3. +32 −0 spec/fake_braintree_spec.rb
View
@@ -78,3 +78,32 @@ will fail). Setting `verify_all_cards!`, on the other hand, will prevent
creation of customers with bad credit cards - Braintree won't even get to trying
to charge them.
+## Generating transactions
+
+You can generate a transaction using `FakeBraintree.generate_transaction`. This
+is for use in testing, e.g.
+`before { user.transaction = FakeBraintree.generate_transaction }`.
+
+It takes the following options:
+
+* `:subscription_id`: the ID of the subscription associated with the transaction.
+* `:amount`: the amount of the transaction
+* `:status`: the status of the transaction, e.g. `Braintree::Transaction::Status::Failed`
+
+Any or all of these can be nil, and in fact are nil by default.
+
+Full example:
+
+ transaction = FakeBraintree.generate_transaction(:amount => '20.00',
+ :status => Braintree::Transaction::Status::Settled,
+ :subscription_id => 'foobar')
+ p transaction
+ # {
+ # "status_history" =>
+ # [{
+ # "timestamp" => 2011-11-20 12:57:25 -0500,
+ # "amount" => "20.00",
+ # "status" => "settled"
+ # }],
+ # "subscription_id" => "foobar"
+ # }
View
@@ -99,6 +99,14 @@ def self.credit_card_from_token(token)
end
end
+ def self.generate_transaction(options)
+ history_item = { 'timestamp' => Time.now,
+ 'amount' => options[:amount],
+ 'status' => options[:status] }
+ {'status_history' => [history_item],
+ 'subscription_id' => options[:subscription_id] }
+ end
+
private
def self.set_configuration
@@ -105,3 +105,35 @@ def write_to_log
expect { FakeBraintree.failure_response }.not_to raise_error
end
end
+
+describe FakeBraintree, ".generate_transaction" do
+ it "returns a hash with a status_history key" do
+ FakeBraintree.generate_transaction(:amount => '20').should have_key('status_history')
+ end
+
+ context "status_history" do
+ it "has a timestamp of Time.now" do
+ Timecop.freeze do
+ transaction = FakeBraintree.generate_transaction(:status => Braintree::Transaction::Status::Failed,
+ :subscription_id => 'my_subscription_id')
+ transaction['status_history'][0]['timestamp'].should == Time.now
+ end
+ end
+
+ it "has the desired amount" do
+ transaction = FakeBraintree.generate_transaction(:amount => '20.00')
+ transaction['status_history'][0]['amount'].should == '20.00'
+ end
+
+ it "has the desired status" do
+ status = Braintree::Transaction::Status::Failed
+ transaction = FakeBraintree.generate_transaction(:status => status)
+ transaction['status_history'][0]['status'].should == status
+ end
+
+ it "includes the subscription id" do
+ transaction = FakeBraintree.generate_transaction(:subscription_id => 'foobar')
+ transaction['subscription_id'].should == 'foobar'
+ end
+ end
+end

0 comments on commit 4cb5cb0

Please sign in to comment.