I extract the plugin from a real project
http://code.google.com/p/railsjob/ to make test easier.
I submit it to http://agilewebdevelopment.com/plugins/test_helpful
And write a short document -- sorry for my ugly English. :p
TestHelpful
===========
This plugin provide the following helper method for test
# Functional Matrix Test support
To knowlearn about it: http://www.infoq.com/news/2007/04/matrix-your-tests
This plugin modify the matrix test code of ZenTest and add some new feature.
* You need not to define matrix_init_xxx method, but init with block:
In your test case:
def setup
init(:logined){ login_as :dongbin}
end
then "login_as :dongbin" will be called when setup include "logined"
* Reuse your test with after_filter
In your test case
after_filter :render_form, :new, :edit
def matrix_test_render_form
assert_response :success
assert assigns(:job)
assert_form(assigns(:job), *@@job_default_attr.keys)
end
def matrix_test_new
assert_template 'new'
end
def matrix_test_edit
assert_template 'edit'
end
Then matrix_test_render_form will be called after matrix_test_new or matrix_test_edit
# assert_form
Note: This method can only work with edge rails(Rails 2.0)
assert_form(@user, :email, :password)
equals to
assert_select("form[action=/users]") do
assert_select("#user_email[name=?]", "user[email]")
assert_select("#user_password[name=?]", "user[password]")
assert_select("input[type=submit]")
end
# Add a method named "create" for unit tests and functional tests
* For Unit Test
in UserTest
create(:name => 'dongbin')
equals to
User.create(@@user_default_attr.merge(:name => 'dongbin')
If @@user_default_attr not defined, then it is an empty {}
* For functional test
In UserControllerTest
create(:name => 'dongbin')
equals to
post :create, :user => @@user_default_attr.merge(:name => 'dongbin')
If @@user_default_attr not defined, then it is an empty {}
Example
http://railsjob.googlecode.com/svn/trunk/test/functional/jobs_controller_test.rb
Copyright (c) 2007 Bin Dong<dongbin.cn@gmail.com>, released under the MIT license
I submit it to http://agilewebdevelopment.com/plugins/test_helpful
And write a short document -- sorry for my ugly English. :p
TestHelpful
===========
This plugin provide the following helper method for test
# Functional Matrix Test support
To knowlearn about it: http://www.infoq.com/news/2007/04/matrix-your-tests
This plugin modify the matrix test code of ZenTest and add some new feature.
* You need not to define matrix_init_xxx method, but init with block:
In your test case:
def setup
init(:logined){ login_as :dongbin}
end
then "login_as :dongbin" will be called when setup include "logined"
* Reuse your test with after_filter
In your test case
after_filter :render_form, :new, :edit
def matrix_test_render_form
assert_response :success
assert assigns(:job)
assert_form(assigns(:job), *@@job_default_attr.keys)
end
def matrix_test_new
assert_template 'new'
end
def matrix_test_edit
assert_template 'edit'
end
Then matrix_test_render_form will be called after matrix_test_new or matrix_test_edit
# assert_form
Note: This method can only work with edge rails(Rails 2.0)
assert_form(@user, :email, :password)
equals to
assert_select("form[action=/users]") do
assert_select("#user_email[name=?]", "user[email]")
assert_select("#user_password[name=?]", "user[password]")
assert_select("input[type=submit]")
end
# Add a method named "create" for unit tests and functional tests
* For Unit Test
in UserTest
create(:name => 'dongbin')
equals to
User.create(@@user_default_attr.merge(:name => 'dongbin')
If @@user_default_attr not defined, then it is an empty {}
* For functional test
In UserControllerTest
create(:name => 'dongbin')
equals to
post :create, :user => @@user_default_attr.merge(:name => 'dongbin')
If @@user_default_attr not defined, then it is an empty {}
Example
http://railsjob.googlecode.com/svn/trunk/test/functional/jobs_controller_test.rb
Copyright (c) 2007 Bin Dong<dongbin.cn@gmail.com>, released under the MIT license
![](http://c.services.spaces.live.com/CollectionWebService/c.gif?space=dongbinspace&page=RSS%3A+test_helpful+released+--+A+plugin+on+rails+to+make+test+easier&referrer=)
![](https://i-blog.csdnimg.cn/blog_migrate/1ed30ca190b019fc8f0f677901a45ffd.gif)