Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails testing controller private method with params

I have a private method in a controller

private 
  def body_builder
    review_queue = ReviewQueueApplication.where(id: params[:review_queue_id]).first
    ...
    ...
  end

I would like to test just the body_builder method, it is a method buidling the payload for an rest client api call. It needs access to the params however.

describe ReviewQueueApplicationsController, type: :controller do
  describe "when calling the post_review action" do
    it "should have the correct payload setup" do
      @review_queue_application = ReviewQueueApplication.create!(application_id: 1)
      params = ActionController::Parameters.new({ review_queue_id: @review_queue_application.id })
      expect(controller.send(:body_builder)).to eq(nil)
    end
  end
end

If I run the above it will send the body_builder method but then it will break because the params have not been set up correctly as they would be in a call to the action.

I could always create a conditional parameter for the body_builder method so that it either takes an argument or it will use the params like this def body_builder(review_queue_id = params[:review_queue_id]) and then in the test controller.send(:body_builder, params), but I feel that changing the code to make the test pass is wrong it should just test it as it is.

How can I get params into the controller before I send the private method to it?

like image 721
TheLegend Avatar asked Oct 18 '22 10:10

TheLegend


1 Answers

I think you should be able to replace

params = ActionController::Parameters.new({ review_queue_id: @review_queue_application.id })

with

controller.params = ActionController::Parameters.new({ review_queue_id: @review_queue_application.id })

and you should be good. The params is just an attribute of the controller (the actual attribute is @_params but there are methods for accessing that ivar. Try putting controller.inspect in a view).

like image 158
niels Avatar answered Nov 03 '22 07:11

niels