7989

How do I test this code in RSpec?

Question:

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

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

Answer1:

How about:

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

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

get 'some_ajax_action'

then the first one would be xhr :get, :some_ajax_action, :id => "37".

Recommend

  • Add new users from a text file in bash script
  • Django_permissions, how to template my menu item based on the permissions?
  • How to find @object and then duplicate @object?
  • How to access params in post request
  • Rails and Amazon S3
  • Rails 5.1.: destroy records in “has_many: through” association with restriction
  • Called id for nil, which would mistakenly be 4 — if you really wanted the id of nil, use object_id
  • Unfortunately this works: ROR Comparison in Activerecord - update
  • Setting an object in the Django cache API fails due to pickle error
  • Active record has_many association for one model having two types of values
  • retrieving username with find_by_id(comment.user_id).name not working
  • Unit Test case using Rspec for each loop
  • Make mongoid session read only
  • Matlab to Python Conversion binary file read
  • Wrap C++ function using Boost Reflect or another C++ reflection library
  • android Navigation Bar hiding and persantage of usable screen overlap
  • Sending cookie value via httpget but not getting the desired response
  • hide missing dates from x-axis ggplot2
  • Find Previous month name using Calender or any classes that in java
  • How integrated is Collada to OpenGL ES
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • Textfile Structure (tables)
  • Rails Find when some params will be blank
  • ActiveRecord query for a count of new users by day
  • Email format validation in mvc3 view
  • Cannot connect to cassandra from Spark
  • req.body is undefined - nodejs
  • Modifying destination and filename of gulp-svg-sprite
  • Fill an image in a square container while keeping aspect ratio
  • Importing jscolor library in angular 2
  • Font Awesome Showing Box instead of Icons
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How to stop GridView from loading again when I press back button?
  • Is it possible to post an object from jquery to bottle.py?
  • Can't mass-assign protected attributes when import data from csv file
  • Python/Django TangoWithDjango Models and Databases