如何测试这个code在RSpec的? [英] How do I test this code in RSpec?

查看:88
本文介绍了如何测试这个code在RSpec的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是只有通过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

这篇关于如何测试这个code在RSpec的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆