这是在Ruby on Rails 3.2.2中仅通过ajax调用的方法
def some_ajax_action
@user = User.find_by_id(params[:id])
@user_list = User.find_all_by_parent_id(params[:id])
respond_to do |format|
format.js
end
end
我想知道用RSpec测试它是否必要(或可能)?有什么想法吗?
答案 0 :(得分:2)
怎么样:
before do
User.stub(:find_by_id)
User.stub(:find_all_by_parent)
end
it "finds the user" do
User.should_receive(:find_by_id).with("37")
xhr <HTTP VERB>, :some_ajax_action, :id => "37"
end
it "finds the user list" do
User.should_receive(:find_all_by_parent).with("37")
xhr <HTTP VERB>, :some_ajax_action, :id => "37"
end
将<HTTP VERB>
替换为routes.rb
中针对这些ajax调用的内容,例如如果你有
get 'some_ajax_action'
然后第一个是xhr :get, :some_ajax_action, :id => "37"
。