I want to test the Ransack part in Rails Rspec, but I get an error saying undefined method `[]'for nil: NilClass

Asked 2 years ago, Updated 2 years ago, 84 views

This is a question about Rspec.
I'm writing the code for the Rspec test to see if Ransack is working properly, but in the simulation section of the search execution,

NoMethodError:
undefined method `[]' for nil: NilClass

appears.

The
 file is as follows:

spec/controllers/master_costs_controller_spec.rb

require 'rails_helper'

describe MasterCostsController do
  describe'GET#index'do


    context 'when master_costs searched'do
      before do
        create_list(:master_cost,10)#data for search
      end

      specify do
        @params[:q][:name_cont] = 'Ahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
        get —index, @params
        expect(assigns(:master_costs)) .to match_array([@data1])
      end
    end 
  end    
end

spec/factories/master_costs.rb

FactoryGirl.definedo
  factory —master_costdo

    sequence(:code)
    sequence(:name) {|i | "MasterCostName#{i}"}
    cost_class {MasterCost.cost_classes.values.sample}
    buffer_class {MasterCost.budget_classes.values.sample}

    @params=Hash.new,
    @params[:q] = Hash.new
  end
end


FactoryGirl.definedo
  before: each, class: MasterCost do
    @data1=create(:data,code:1,name:'Ah',cost_class:1,budger_class:'A')
    @data2=create(:data,code:2,name:'good',cost_class:2,budger_class:'AB')
    @data3=create(:data,code:3,name:'u',cost_class:3,budger_class:'A')
    @params=Hash.new
    @params[:q] = Hash.new
  end
end

Error Log

Failures:

  1) MasterCostsController GET# index when master_costs searched
 Failure/Error: @params[:q][:name_cont] = 'Ah'

 NoMethodError:
   undefined method `[]' for nil: NilClass
 # ./spec/controllers/master_costs_controller_spec.rb: 28: in `block(4 levels) in <top(required)>'

Finished in 0.37994 seconds (files book 9.51 seconds to load)
3 examples, 1 failure

Failed examples:

rspec./spec/controllers/master_costs_controller_spec.rb:27 #MasterCostsController GET #index when master_costs searched

Randomized with seed 65361

I'm sorry to trouble you, but if you understand, I'd appreciate it if you could give me a little hint.
@params[:q][:name_cont] = 'Ahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh

ruby-on-rails ruby rspec

2022-09-30 21:16

1 Answers

Now that I've solved it myself, I'll let you know just in case.

 Failure/Error: @params[:q][:name_cont] = 'Ahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
 NoMethodError:
   undefined method `[]' for nil: NilClass 

=> No hash object

All causes are

in factors/master_costs.rb.
FactoryGirl.definedo
  before: each, class: MasterCost do
    @data1=create(:data,code:1,name:'Ah',cost_class:1,budger_class:'A')
    @data2=create(:data,code:2,name:'good',cost_class:2,budger_class:'AB')
    @data3=create(:data,code:3,name:'u',cost_class:3,budger_class:'A')
    @params=Hash.new
    @params[:q] = Hash.new
  end
end

of
@params=Hash.new
@params[:q] = Hash.new


Place the
Navigate to master_costs_controller_spec.rb

context'when master_costs searched'do
  before do
    create_list(:master_cost,10)#data for search
  end
  before do
    @data1=create(:master_cost, code:112, name: 'Ahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh)
  end
  specify do
    @params=Hash.new
    @params[:q] = Hash.new
    @params[:q][:name_cont] = 'Ahhhhhhhhhhhhhhhhhhhhhhhhhh
    get —index, @params
    expect(assigns(:master_costs)) .to match_array([@data1])
  end
end

Then everything was solved.I mistook the error statement '[]' for an array, not a hash, so it took some time.


2022-09-30 21:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.