1

I'm new to RSpec and I want to test if-else condition of a controller, I searched on the internet but the found results are not properly satisfying my search query.

Here is my rails controller:

class ScheduleTimeSlotsController < ApplicationController
  def get_available_schedules
    if params[:for_date].to_date==Date.today
      available_schedules=ScheduleTimeSlot.where(:doctor_id => params[:dr_id],:date=>params[:for_date].to_date,:status=>"vacant",:archive=>false).where("start_time >=?",Time.now.seconds_since_midnight).reorder(:start_time)
    else
      available_schedules=ScheduleTimeSlot.where(:doctor_id => params[:dr_id],:date=>params[:for_date].to_date,:status=>"vacant",:archive=>false).reorder(:start_time)
    end
    render :json =>{:available_schedules=>available_schedules}
  end
end

I'm using ruby version 2.2.4 and rails version is 4.2.0

Please guide me that how do I write RSpec for this condition, with the help of an example.

Thanks in advance.

7
  • Your conditions depends on params so you can check your if - else conditions by passing your params once correct and next time incorrect for respective test scenario. Commented Aug 23, 2016 at 6:47
  • Your controller's code is incomplete, please attach the whole code, and only then will someone be able to write the appropriate test. Commented Aug 23, 2016 at 7:28
  • @amingilani I've now added the whole exact code, now answer me. You can explain this condition by taking any example. Commented Aug 23, 2016 at 7:37
  • @VishalNagda don't forget to mark this question as answered, if you found the answer. Commented Aug 24, 2016 at 5:32
  • well!! this is not the exact answer. but with the help of this answer if i get the specific solution later then i will mark it as answered afterwards. please don't worry about this. i will get to you soon. Commented Aug 25, 2016 at 16:52

1 Answer 1

2

You'll need to run two tests for your controller. In one pass a date that is today, and one that isn't and test the responses. Here:

require 'rails_helper'

RSpec.describe ScheduleTimeSlotsController, type: :controller do
  describe 'GET #get_available_schedules when for_date is today' do
    it 'returns http success' do

    for_date = Date.today
    available_schedules = ScheduleTimeSlot.where(
      doctor_id: params[:dr_id],
      date: params[:for_date].to_date,
      status: 'vacant',
      archive: false
    ).where(
      'start_time >=?',
      Time.now.seconds_since_midnight
    ).reorder(:start_time)

      get 'get_available_schedules', for_date: for_date
      expect(response.body.available_schedules).to eq(available_schedules)
    end
  end

  describe 'GET #get_available_schedules when for_date is not today' do
    it 'returns http success' do

    for_date = Date.yesterday
    available_schedules = ScheduleTimeSlot.where(
      doctor_id: params[:dr_id], date: params[:for_date].to_date,
      status: 'vacant',
      archive: false
    ).reorder(:start_time)

      get 'get_available_schedules', for_date: for_date
      expect(response.body.available_schedules).to eq(available_schedules)
    end
  end
end

Disclaimer: this code is untested, since I don't have access to the actual app.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.