我有以下控制器动作和测试。我是使用 Shoulda 进行测试的新手,我知道我的控制器的某些区域可以进一步测试。例如,我的 Flash 消息以及验证渲染。
所以我的问题是,我将如何在 Shoulda 中正确测试此控制器操作?
我的控制器操作(名称已更改以保护无辜者):
def my_action
return redirect_to(root_url) if @site.nil?
@owner = current_site.owner
if request.post?
if params[:password].blank? || params[:email].blank?
flash[:error] = "You must fill in both the e-mail and password fields"
render :action => "my_action"
else
if @owner.authenticated?(params[:password])
@owner.login = params[:email]
@owner.save!
@owner.do_some_method
flash[:success] = "Success."
render :action => "my_action"
else
flash[:error] = "Incorrect password"
render :action => "my_action"
end
end
end
end
我的测试:
context "on POST to :my_action" do
setup do
Owner.any_instance().expects(:do_some_method)
post :my_action, :email => 'foo@bar.com', :password => 'test'
end
should_assign_to :owner
should "Change name and verify password and resend activation key" do
assert_equal true, assigns(:owner).authenticated?('test')
assert_equal 'foo@bar.com', assigns(:owner).login
end
should_respond_with :success
end