The fake_braintree gem makes your life easier when testing code that charges credit cards. It is a fake Braintree server that can be manipulated as you see fit.
Start by installing fake_braintree. In your Gemfile:
gem 'braintree'
group :test do
gem 'fake_braintree'
end
And in your spec/spec_helper.rb:
require 'fake_braintree'
RSpec.configure do |config|
config.before do
FakeBraintree.clear!
end
end
That RSpec configuration clears the fake data between test runs.
Now we can write a quick spec for Purchase#transact:
require 'spec_helper'
describe Purchase do
it 'charges successfully' do
transact.should be_true
end
it 'fails when the card is declined' do
FakeBraintree.decline_all_cards!
expect { transact }.to raise_error
end
def transact
Purchase.new.transact
end
end
You can see fake_braintree peeking through there in the second spec, where we decline all credit card transactions. Since we clear fake data between test runs, this does not affect other tests.
We can make this pass as normal:
class Purchase
def transact
result = Braintree::Transaction.sale(
amount: '1000.00',
credit_card: {
number: '5105105105105100',
expiration_date: '05/12'
}
)
result.success? or raise result.errors.inspect
end
end
The fake_braintree gem is basically invisible until you need it. The Braintree implementation code is unchanged, and the success paths are the default.
You can set fake_braintree to verify or outright decline credit cards (as above), and you can also generate transactions as if the user really did buy something.
So stop being so hands-on with your credit card mocking and stubbing and give fake_braintree a try today!
Version 1.1.2 of bourne is out (fba748b), without a NEWS file describing the changes! However, the history indicates that it is compatible with a more recent version of mocha and it presents better error messages now.
We’re trying out version 2.0.0, release candidate 1, of bourbon (0ec0115). So please, try it out and let us know what’s broken.
Out of nowhere, Gabe Berke-Williams (gabebw) changed the formatting of something (7ec8959).
But Phil LaPier (plapier) removed animation-basic (316f2a7), cleaned up the default padding for buttons (2a46225), changed the radial-gradient in a way I don’t totally understand (94506ea), added some shorthands for transition (3342577), removed the position mixin (4621347), and updated the README (5f1176d and f5cdfad).
Version 3.0.0 of factory_girl is out: read the blog post to know more. In summary: goodbye five-year-old Ruby 1.8, goodbye two-year-old Rails 2, goodbye four-year-old FactoryGirl syntax!
Minor cleanups to suspenders this week: Gabe Berke-Williams (gabebw) fixed some markdown (107b103) and Andy Waite (andyw8) removed some annoying whitespace probably left by someone using TextMate (32104f9) and noted the fact that you must be running Postgres while suspending (de20630).
In paul_revere news this week, Cameron Desautels (camdez) pointed out that controller helpers need to use the helper class method (3b74d84).
The shoulda gem got taken care of by Gabe Berke-Williams (gabebw) this week: the Rails test helpers use the installed shoulda-matchers and shoulda-context gems instead of the GitHub ones (6f3b7b4); the dependencies are updated to more recent versions, and the tests require the user of bundler now (34d9f4a); the Rakefile doesn’t present a clobber action, whatever that did (058724b); and the contribution guidelines now reflect the fact that we’ve wanted pull requests for the past three years (f75da74).
This week, Gabe decided to take all pull requests on shoulda-matchers over the past few…years. Gabe Berke-Williams (gabebw) also did a bunch of cleanup and maintainance (c10cf34, 31b140f, c27de41, 7d70805, 7950d00, 3d6b8a8, ff0d364, ca6d428, ba78ed0, 8724cd2, c493c40, f841805, 0746b79, 963a293, 65d6d1c, afaf72b, and 76127ea).
Norway’s own Håkon Lerring (Hakon) added as_instance_of to the serialize matcher (9b5e972, 3b891cf, and d9906d8). Hey Håkon, I’m moving to your neighboring country soon!
Matthew Daubert (MDaubs) added an odd query_the_database matcher, which could be useful for making sure a n+1 is good and dead (0ecb774, 93cd34e, 56de191, 8e7fbc5, 558d1df, and 00a9a68).
Frank Luithle (sigi) extended the allow_value matcher to handle multiple values (cab62a1, c86d8e2, and 026927b).
Luca Guidi (jodosha) updated our dependencies. Sweet. (1956736)
And last but not least, Mike Bloy (bloy) extended the association matchers to understand inverse relations and to check the correct class (a4cb684, 5c173bf, and c863f7e).
The capybara-webkit gem got a fix for the RSpec matcher from Chris Nicola (lucisferre), possibly due to an API change elsewhere (8fb7ccd); Will Ryan added keyCode and which to the keypress, keydown, and keyup triggers (c6103e7 and fb97fe8); and Joe Ferris (jferris) refactored everything (1ed54b1, 3ebe0fa, 67d5e3d, 354180a, 85dcc72, and 5b57eee), plus stopped clearing the page on the URL about:blank (b3b1adf).
A bug fix to dotfiles this week as Claudio Ortolina (cloud8421) changed the .vimrc to load the local configs from the homedir instead of just about anywhere (9dc7f9c).
A feature in appraisal this week: Gregory Ostermayr (gregors) exposed the bundle command that Appraisal uses to install the Gemfile-specific gems (6b41d07 and 8023273). Gabe Berke-Williams (gabebw) stopped the tests from loading shoulda; instead it loads rake, which is already installed (beaee3b).
The contribution guidelines were written for fake_braintree by Gabe Berke-Williams (gabebw), which are mostly the same as our others (bd5220a). Namely, we love tests and pull requests.
Mostly quiet on the paperclip front, except Dmitry Polushkin (dmitry) snuck a backward-incompatible change into the generated migration (b7bfdec).
shoulda is version 3.0.0! Someone needed to cut it, and Joe Ferris (jferris) stepped up to make the release (4f0c363).
A new version of high_voltage is out this week, with contributions from Matthew Horan (mhoran). Matt fixed the content_path hack on PagesController such that you can subclass and override the content path properly (0d84952, 9c7c0ad, 8884f70, and d4ba056, e4b1ce9). He also added Rails 3.1 compatibility (7556fcd). Joe Ferris (jferris) pushed version 1.1.1 out (09c2da4, a60cc5b, and a1f2476).
What’s new in version 1.4.0 of bourbon? Glad I asked myself, in writing. Phil LaPier (plapier) changed buttons from display: inline to display: inline-block, removing text decoration on anchor buttons (8bea3a7). He executed the “extract mixin” Bourbon refactoring pattern on render-gradients (9fd50d1), moving the linear and radial gradients to the root (92ecae3), all so he could support more gradients in the border-image mixin (0f91303). Transparent fallback colors are now allowed (b409982 and 5d86ebd), a new user-select mixin was added (b00d334), and finally Phil bumped the version (f9ee911).
A new version of paperclip is out—version 2.7.0 was pushed by Mike Burns (mike-burns) to Rubygems (760a4e0).
Mike—that’s me—flipped out at a failing test and reverted the :swallow_stderr support that was pull in last week (04c9514). The tests didn’t work and the code was ugly. As a general request: if you’re adding code to an ugly codebase, add beautiful code!
Prem Sichanugrist (sikachu) updated the docs to point out that awk-sdk must be a specific version (22a2a85). He also added support for Rails 3.2 (77c5268).
Tom Hughes (tomhughes) noted that the fingerprint field is not cleared when an attachment is deleted, though all other fields are…and he fixed that (189ee9c). Pat George (pcg79) finally fixed the failure message for validates_attachment_size, which was written very incorrectly, for years (45b7dac).
Nothing new on kumade. Gabe Berke-Williams (gabebw) stopped ignoring the Gemfile.lock, despite what Yehuda suggests (a2a2278); simplified the name of a scenario (ffd1aef); and set up Travis CI to run a full integration test (04d9f6b).
The shoulda-matchers project had a very ancient list of contribution guidelines so I, Mike Burns (mike-burns), updated them to reflect the fact that pull requests do not actually suck (81cd02b).
More tiny commits on bourne from Gabe Berke-Williams (gabebw), where he added Travis CI support (55248d6) and fixed some code formatting (f31aa72). Yup.
The only news for capybara-webkit this week is that Joe Ferris (jferris, a.k.a. JƒF) does not want bug reports about system-specific compilation issues (6b724ef). We simply can’t fix them.
Nothing new on clearance, but Dan Croak (croaky) did get it working under Travis CI (3430e88).
Tiny cleanups on appraisal as Gabe Berke-Williams (gabebw) changed some parens around and removed some of those awful if modifiers (7a5af59), and Brian Cardarella (bcardarella) updated the README to use actual English (1883b80).
A somewhat short update this week, in large part because I’m writing this from a different timezone than normal. Next week I’ll be five hours slower, don’t you worry!
New in suspenders, our Rails app generator: a pre-packaged email validator class (b91ea65), which probably mixes well with things like liaison. Also on the email front, delivery errors are a full error in development, which means we can catch errors faster (637427d). Both of these improvements brough to you by Dan Croak (croaky).
The shoulda-matchers collection of RSpec/shoulda matchers has hit 1.0.0! Gabe Berke-Williams (gabebw) made the switch from 1.0.0.beta3 to 1.0.0 (7c408ef), but first removed a deprecation warning from the rdoc generator (c47557c) and simplified the test suite (22740fd).
Another 1.0.0 release for shoulda this week, as shoulda-context, which controversially provides nested test structures for Test::Unit saw an explicit dependency on Test::Unit so that MRI 1.9.2 doesn’t confuse it with MiniTest (d1c5881)—that’s thanks to Ryan McGeary (rmm5t)—but then Jason Morrison (jasonm) decided that enough was enough and he released version 1.0.0 of shoulda-context (9557425).
Our file uploading gem, paperclip, saw an interesting commit from yours truly, Mike Burns (mike-burns). With a Paperclip attachment you can get the URL (e.g. @user.avatar.url(:medium)). The logic for generating this URL was specified in the Paperclip::Attachment class, which meant that to do anything interesting like delaying the thumbnail generation or storing the style names in the database involved monkey-patching, which is clearly a mistake. So I pulled that functionality out into the UrlGenerator class, but more importantly I parameterized that class name so you can pass whatever you want. For example:
class User < ActiveRecord::Base
has_attached_file :avatar,
:url_generator => DataBaseStyledUrlGenerator.new(ActiveRecord::Base.connection)
end
class DataBaseStyledUrlGenerator
def initialize(db)
@db = db
end
def new(attachment, attachment_options)
@attachment = attachment
@attachment_options = attachment_options
end
def for(style_name, options)
dimensions = db.select_row("SELECT width, height FROM styles WHERE style_name = ?", style_name)
"/assets/images/#{dimensions['width']}/#{dimensions['height']}/#{@attachment.id}.jpg"
end
end
Refactoring to add functionality? Heck yeah! (bc5c51d)
A bug fix and the usual slew of refactorings for factory_girl this week. As the bug fix, Joshua Clayton (joshuaclayton) fixed implicit traits’ error output by having them know their name (0124d42). Small, but good to have.
Internally, he applied the null object pattern to act as a parent of orphan factories (aee300a). He added a DeclarationList to hold the collection of things declared about a factory, including attributes (3114dcd and f721bc6). He abstracted out the idea of a factory and a trait into the Definition class (41bc3ac). He also removed the unused @ignored_attributes instance variable (68e8e9e) and moved the logic around adding a proxy value into one place (3803be3).
Alexander Gronemann (Highcode) fixed a typo in the docs for us (5a48129). Thanks!
All changes to the capybara-webkit headless browser were documentation changes this week: Joe Sak (joemsak) mentioned :js => true for specs and that transactional fixtures still cannot be used (eb6165d). Joe Ferris (jferris) took a stance on the inconsistent authors section of the README by specifying his name and Matt Mongeau’s name, but also pointing to the list of Github contributors (8674ef7). While there he changed some grammar to his liking (7be8f94).
Another refactoring-based feature for us this week in clearance, our authentication gem. Constantine Mavromoustakos (cmavromoustakos) split the user instance methods into an InstanceMethods module, which helped them be more selective about which methods they want to pull into their MongoDB-based app (cc0d007).
Mostly refactoring on the Braintree test fakes, fake_braintree, this week, but let’s see what features we can pull out of these refactorings by Gabe Berke-Williams (gabebw). He introduced a FakeBraintree::Subscription class (e4e99f8), which can you use as a fake subscription. He also introduced a FakeBraintree::Customer class, to be a fake paying customer (0259b9d and 9902e08). Maybe you needed those!
On the lighter side he cleared up the test coverage question by removing most untested code (0f6ed3f and ad7a4b9), adding a test to ensure that the next billing date is a month from now (84cfbc4), and removing an 80-line unused method (e1a886c). He responded to my ctags file observation from last week by removing it (8eb5988 and 7a9fb76). He also did a couple of other things under the name “cleanup” (5690d40, 76675cb, 2aaf85c, d7811aa, and cc7463f).
This week our Heroku deployer, kumade, saw some refactoring and some features. Gabe Berke-Williams (gabebw) marked slow cucumber tests as @slow (40c6803), fixed the check for the Heroku Cedar stack (4b7358e, 3180ad2, and 61d3ee8), and cleaned up the README (c0d12cb). He removed the intrusive Thor from the app (dc25839, e419061, and 0c8414d). He ensured the the no-op packager is never installed (0e4cdc3) and added a COVERAGE environment flag to produce code coverage output (bda3067 and e260283).
Gabe and hoverlover (hoverlover) updated Kumade to restart the Heroku app after migrating (99a14b7, e9aa04a, and 781dd0d) and then Gabe released version 0.7.0 of kumade (60fb782 and f1ac4c3).
We have a gem that makes Rails apps, named suspenders. There’s even a nice mascot to go along with it. Anyway, this week Dan Croak (croaky) cleaned it up a bit: he upgraded clearance to 0.13.0 (6c2d39f), defaulted to ruby-debug for MRI 1.9.x (56fd39e), upgraded cucumber-rails to the controversial 1.1.0 (e2a8f47), added thin for Heroku (9be97e7 and 4c78926), removed RedCloth (91ec5a8), updated the README to explain the system dependencies (9900d15), and changed the template to output explanations with less emotion (4bab3f0). It’s in better shape now!
New features this week in our collection of RSpec matchers, shoulda-matchers! Vasiliy Ermolovich (nashby) brings us it { should ensure_exclusion_of(:age).in_range(30..60) } (d8308ec). He also brought in the :partial option for the render_template matcher (16eb4aa). Speaking of options, Markus Schwed (SweeD) sent a class_name option for the association matcher, like it { should have_many(:friends).class_name('Buddy') } (17ac7d5).
Dan Croak (croaky) removed the deprecated rake/gempackagetask in favor of rubygems/package_task (ead1e1d) and ironically removed the word “should” from the tests for the render_template matcher (030da4a).
Short week for bourbon, the collection of Sass mixins: Phil LaPier (plapier) added a border-image mixin and released version 1.1.0 of bourbon (80027f2).
Also a short, but important, week for the pacecar collection of named scopes, when Thomas Dippel (dipth) fixed the spelling of the word “maximum” in the README (91f56cb).
Lots of love to our paperclip file uploader. Alexander Presber (momolog) exposed the S3 permissions data (f409693), useful for testing and fixing a bug in them (3a6ca7d and 1571290). Joern Barthel (yawn) exposed the uploaded file as uploaded_file> (24b0c5a), and then Overbryd (Overbryd) decided to only write the uploaded file if the uploaded file is present (7cc0921). Ken Collins (metaskills) fixed the fog_public attribute reader to produce the false value as needed (de9a94a). ramn (ramn) made single-table inheritence, cached classes, and paperclip work together again (2c6b7cb).
In features, Tim Assmann (aka47) made it such that the S3 permissions could be a Proc object in addition to a normal hash, for run-time configurability (2191934). Kelley Reynolds delayed the S3 connection until the user directly accessed the S3 object or bucket (774cee1).
Prem Sichanugrist (sikachu) and Jon Yurek (jyurek) have been cleaning and fixing paperclip: Prem cleaned the whitespace (64549cb), copied the rake tasks into place on Rails 2.3 (948ddfa), and fixed the paperclip:clean rake task to run again without errors (cd856aa). Jon attempted to fix the URL autoescaping (131a8b1 and 4917ab2), fixed validates_attachment_presence (cd53485 and 61a0536), and added backward compatibility for Attachment#url (93faccf).
Then Jon released paperclip 2.4.5 (94d35f9 and 75ba69d).
Let’s start with the refactorings this week for our test fixture gem, factory_girl. Joshua Clayton (joshuaclayton) moved the proxy strategy checks to the proxy class itself (a09ec68). He removed some private methods by leaning on Hash#except from ActiveSupport (eb73dc1). He removed the parse_method abstract factory method, removing more lines than he added (50a66d0). In an effort to reduce conditionals he now always passes callbacks to proxies (8f4e052). Some method names were changed (369b4b9), a private Factory::Runner class was made to abstract out the actual factory creation (105d972), some whitespace was removed (65fc7d7), and ActiveSupport’s delegate method was used to remove some method definitions (9e88eb1).
Featurely, attribute lists are now named, which lead to them checking for infinite loops (e3bf491). Registries of factories, traits, and sequences are now named, too, leading to better error messages (7fdf1a8). The spec for a proxy definition was cleaned up with a MockFactory class and some exposed matchers; you may find these useful if you are building a gem atop FactoryGirl (6e9baa7 and 180eb8b).
Outside of Josh’s commits, Gabe Berke-Williams (gabebw) got rcov working (895bc08) and Paco Guzman (pacoguzman) totally called us out on using our Attribute::Dynamic class incorrectly (e1ece10).
And then Josh released FactoryGirl 1.3.0 (1267f5a and bf6acea).
A whole boatload of commits to our sweet JavaScript test runner named capybara-webkit. On the refactoring front, Gabe Berke-Williams (gabebw) removed unused global variables from the self-signed cert spec (b4faa3a). OK, check.
In terms of fixing bugs, Matthew Mongeau (halogenandtoast) set the viewport size so overflow: hidden registered visibility properly (8d2251d). He also made sure that only processes that started the webkit server could stop it (3eb366e). Andrew Wason (rectalogic) fixed the C++/Qt to have the proper signature for connect (9cfc6b1). Niklas Baumstark (niklasb) noted that IO.popen takes a string in Ruby 1.8.7 (3b872c6).
Then onto features, Andrew set it up to ignore SSL errors, as desired (f57b3a3, 053010a, and a08e5e8). Niklas added a HTTP proxy option (229a20e, 6a4143c, e442150, 25281b7, d59e36b, a0d4a21, bb80506, and d348e20) and made cookeis easier to access (3bce3d7 and ce198ab).
Progressing forward on our ready-to-use config files: dotfiles. Mark Wolfe (wolfeidau) removed the need for bash (b89501e), which is awesome because I don’t like bash. Thanks, Mark!
Minor infrastuctural change to clearance: Dan Croak (croaky) used the Bundler rake tasks to make new releases (4b4c251). He did not yet make a new release in celebration.