控制器
基本的控制助手方法
如ActiveRecord宏一样,Shoulda 提供了一套测试控制器的宏,以尽可能简洁的方法进行测试。所有的这些方法都在Shoulda的Rdoc中,但这里再送上一个快捷的例子:
class UsersControllerTest < Test::Unit::TestCase
context "on GET to :show" do
setup { get :show, :id => 1 }
should_assign_to :user
should_respond_with :success
should_render_template :show
should_not_set_the_flash
should "do something else really cool" do
assert_equal 1, assigns(:user).id
end
end
context "on POST to :create" do
setup { post :create, :user => {:name => 'Ronald', :party => 'Repukeulan' } }
should_assign_to :user
should_redirect_to "user_url(@user)"
should_set_the_flash_to(/created/i)
end
end
应该RESTful
这里每个 should_xxx 宏都会产生一个单独的测试方法,编写起来又DRY。而should_be_restful 宏可以产生遵循基本的RESTful设计模式的控制器。should_be_restful 宏就像一个超级测试生成器,每次调用是都会产生40到50个测试方法。这里有个超简单的例子:
class UsersControllerTest < Test::Unit::TestCase
def [...]