0

NOTE

I know, that action is reserved word in Rails. I am implementing a third party API for Yandex Money in a Rails server and the Yandex API app is posting an action param into my Rails application. I know how get it (How to not use Rails action parameter in controller). The issue is - how write test to my controller, because my test can't send action parameter (see description below).

I'm writing controller tests in Rails, and I need pass action parameters to the controller from test. This is my controller:

class YandexController < ApplicationController
  def notify
    render text: request.request_parameters
  end
end

When I POST data with curl command:

curl --data "action=paymentAviso&some_other_param=value" http://localhost:3000/yandex/notify

I get correct result:

{"action"=>"paymentAviso", "some_other_param"=>"value"}

But when I try send action parameter in test then it is not working. See my test below:

class YandexControllerTest < ActionController::TestCase
  test "notify sucess" do
    post :notify, { action: 'paymentAviso', some_other_param: 'value' }
    assert_response :success
    puts response.body
  end
end

And when I run test I see:

Running:

{"some_other_param"=>"value"}

Is there a way to pass action parameter to my controller from test?

Community
  • 1
  • 1
stepozer
  • 1,143
  • 1
  • 10
  • 22

1 Answers1

2

You cannot use action in the params unless you are referring to a controller's action. The action parameter is what Rails uses to determine which method to run in the controller. Therefore, when you run your spec, it's just calling paymentAdviso in the controller.

You should change the parameter name so that it does not interfere with Rails' conventions.

For a list of other reserved Rails words, look here: https://reservedwords.herokuapp.com/

Wes Foster
  • 8,770
  • 5
  • 42
  • 62
  • Yes, I understand that is reserved word, but I need integrate with external payment system, that pass `action` parameter in POST request. That is why I must use this parameter name. – stepozer Sep 23 '15 at 15:06
  • You can simply pass `payment_action` to your controller, then take that value and pass it as `action` to your external payment system. You can't argue with Rails :) – Wes Foster Sep 23 '15 at 15:07