1

这是在 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 对其进行测试?有什么想法吗?

4

1 回答 1

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

替换为这些 ajax 调用<HTTP VERB>中的任何内容routes.rb,例如,如果您有

get 'some_ajax_action'

那么第一个就是xhr :get, :some_ajax_action, :id => "37".

于 2012-08-08T06:30:13.210 回答