1

Some of the specs are failing While upgrading ruby, rails versions.

More often for the controller actions which are rendering JSON and XML responses.

Old versions: ruby -v (1.9.3p551), rails -v (3.0.13), rspec -v (2.4.0)

New Versions: ruby -v (2.4.4p296), rails -v (4.2.11), rspec -v (2.99.2)

I have tried by mentioning format as well.

describe "GET index" do
    it "assigns all users as @users" do
        get :index, params, format: :json
    end
end

app/controllers/users_controller.rb

class UsersController < ApplicationController
respond_to :json,:xml, :html

def index
    @users = User.where(:gciunt => params[:gciunt])
        respond_to do |format|
            format.json {render :json => @users, :callback => params[:callback]}
        end
    end
end

spec/controllers/users_controller_spec.rb

require 'spec_helper'

describe UsersController do
    def mock_user(stubs={})
        (@mock_user ||= mock_model(User).as_null_object).tap do |user|
            user.stub(stubs) unless stubs.empty?
        end
    end

    describe "GET index" do
        it "assigns all users as @users" do
            params = {:gciunt => "37"}
            User.should_receive(:where).with(params) { [mock_user] }
            get :index, params, format: :json
            assigns(:users).should eq([mock_user])
        end
    end
end
Error
<b>Failure/Error: get :index, params
ActionController::UnknownFormat:
ActionController::UnknownFormat
# ./app/controllers/users_controller.rb:6:in `index'
# ./spec/controllers/users_controller_spec.rb:15:in `block (3 levels) in <top (required)>'<b>

This error is occurring only after upgrading ruby, rails, RSpec.

Please find the Attached Image here

Community
  • 1
  • 1
  • 1
    try https://stackoverflow.com/questions/11022839/set-rspec-default-get-request-format-to-json . If it does not working try `get :index, params: params` – Jay-Ar Polidario Aug 15 '19 at 15:25
  • Like @Jay-ArPolidario says, add the header. Depends very much on the version of rspec-rails. For example check this one https://github.com/rspec/rspec-rails/blob/8c6c9590b94916199950dc8a91a9741d3be30c7c/features/controller_specs/controller_spec.feature vs this one: https://github.com/rspec/rspec-rails/blob/2-99-maintenance/features/controller_specs/controller_spec.feature the last one is your version. – thelastinuit Aug 15 '19 at 18:41
  • 1
    @Jay-ArPolidario Thank you very much for your help, I was getting some other code related issue like 'stack level too deep' but currently, as of now, I have tried with XML response data and it is working now describe "GET index" do it "assigns all users as @users" do params = {:gciunt => "3333"} User.should_receive(:where).with(params) { [mock_user] } get :index, params.merge({:format => :xml}) assigns(:users).should eq([mock_user]) end end – Aravind Aytha Aug 16 '19 at 14:27
  • @thelastinuit Thank you very much for your help, I was getting some other code related issue like 'stack level too deep' but currently, as of now, I have tried with XML response data and it is working now ` describe "GET index" do it "assigns all users as @users" do params = {:gciunt => "3333"} User.should_receive(:where).with(params) { [mock_user] } get :index, params.merge({:format => :xml}) assigns(:users).should eq([mock_user]) end end ` – Aravind Aytha Aug 16 '19 at 14:28
  • I need to get fix this issue now only in case of JSON, is there any another way please..! – Aravind Aytha Aug 16 '19 at 14:38
  • @AravindAytha a Stack level too deep error suggests to me there's another problem independent of your question. Unfortunately, you'll need to supply more details, or better yet if you're already running out of time, check where the Stack Level error is coming from. If you need better `rspec` logging, run `bundle exec rspec --backtrace` (<---- with the backtrace command line flag). That will show the full trace of the error – Jay-Ar Polidario Aug 16 '19 at 14:49

0 Answers0