gem “data_dumper”, :group => [:development, :test]
You’re knee deep in a debugger session, and you can’t understand why something’s wrong. You wish you could fire up your application against the test database, but sadly, the process which is running the tests is within a transaction, and thus the actual data is opaque. What can you do?
# Somewhere deep in your tests test "the frobble touches the widget" do assert_equal 42, frobble.widget_id end
You’ve been on this
assert_equal call for the past hour wondering. Frustration’s been mounting, because you don’t understand why the frobble doesn’t touch the widget. Clearly, there’s something wrong with the fixtures, but you can’t understand what it is. Time to fire up the debugger and dump the data:
[814, 823] in test/unit/widget_test.rb 814 frobble.save! 815 end 816 817 test "the frobble touches the widget" do 818 debugger => 819 assert_equal 42, frobble.widget_id 820 end 821 822 test "the widget touched the frobble in turn" do 823 assert widget.touched_by_frobble? test/unit/widget_test.rb:819 => 819 assert_equal 42, frobble.widget_id (rdb:112)
data_dumper gem is already declared in your Gemfile (if not, declare it,
bundle install, then run your tests again), type:
(rdb:112) File.mkdir(Rails.root + "dump") (rdb:113) DataDumper.dump(Rails.root + "dump")
Then, quit your failing tests, and from the trusty command line:
$ rails console > DataDumper.load(Rails.root + "dump") > exit $ rails server
Any and all data from your test database will be loaded in your development environment. You can now explore your model with your trusty application, to find out what’s really going on.
This tool is known to work with Rails 2.3.5, PostgreSQL 8.4 and REE 1.8.7 2011.03. Anything else is up for you to test.
(The MIT License)
Copyright © 2011 François Beausoleil (firstname.lastname@example.org)
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
‘Software’), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.