如何测试这个code在RSpec的?测试、code、RSpec

2023-09-10 18:13:59 作者:*你说我不够帅/.

下面是只有通过AJAX中的Ruby on Rails 3.2.2调用的方法

Here is the method that called only via ajax in Ruby on Rails 3.2.2

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

我不知道是不是neccessary(或可能)与RSpec的测试吗?有什么想法?

I wonder is it neccessary (or possible) to test it with RSpec? Any thoughts?

推荐答案

怎么样:

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

替换&LT; HTTP动词GT; 通过无论是在你的 routes.rb中这些Ajax调用,例如:如果你有

Replace <HTTP VERB> by whatever is in your routes.rb for these ajax calls, e.g. if you have

get 'some_ajax_action'

那么第一个是 XHR:搞定了,:some_ajax_action,:ID =&GT; 37