First Release: ActiveTest: Rails-style testing
Posted by Mathew Abonyi Sun, 19 Nov 2006 02:57:32 GMT
I talked about the idea of meta-programming tests in a light way some months ago and explained that repetition especially is a waste of a programmer’s time. Many aspects of my alpha version of ActiveTest have changed since then, so I advise you to go over the README which is included.
For The Impatient
$ cd railsproject
$ ./script/plugin source http://mabs29.googlecode.com/svn/trunk/plugins
$ ./script/plugin install active_testOnce you have ActiveTest installed as a plugin, you can use the subject ActiveTest::Controller for your functional tests. Other subjects you may see in the rdoc documentation are work in progress.
Example of Final Product
The final product of a test using the ActiveTest::Controller subject would look something like this:
class ArticlesControllerTest < ActiveTest::Controller
fixtures :articles
# Each Subject has a setup class method, some of which take options
setup
# Most dynamic methods default to a convention
succeeds_on :index
assigns_records_on :index
succeeds_on :new
assigns_records_on :new
# Options may be given.
succeeds_on :show, :parameters => { :id => 1 }
assigns_records_on :show, :parameters => { :id => 1 }
fails_on :show, :parameters => { :id => 19361 }
# Many options are flexible. Here, you can set parameters to a method or proc which is
# evaluated in the instance scope (Note: it is defined in the class scope).
succeeds_on :create, :parameters => :a_good_article
creates_record_on :create, :parameters => :a_good_article
fails_on :create, :parameters => proc { a_good_article[:article].merge(:body => nil) }
succeeds_on :edit, :parameters => proc {{ :id => articles(:nice_article).id }}
fails_on :edit, :parameters => { :id => 19361 }
# shortcutting a proc which is reused
@update_proc = proc {{ :id => articles(:nice_article).id, :article => { :body => "splat" } }}
succeeds_on :update, :parameters => @update_proc
updates_record_on :update, :parameters => @update_proc
fails_on :update, :parameters => { :id => 19361 }
record_unchanged_on :update, :parameters => { :id => 19361 }
succeeds_on :destroy, :parameters => proc {{ :id => articles(:nice_article).id }}
deletes_record_on :destroy, :parameters => proc {{ :id => articles(:nice_article).id }}
fails_on :destroy, { :id => 19361 }
succeeds_on :empty_collector
assigns_empty_on :empty_collector
protected
# return a hash for parameters
def a_good_article
{:article => { :title => "And now...", :body => "for something completely different" }}
end
endThe advantages of this method is quite simply that you can write less fluff and more tests. Edge cases are defined in the same way, so you can still do your own specific tests without being effected by ActiveTest.
Please use the GoogleCode Issues facility to report bugs you find with this plugin.
