2011-03-31 17 views
2

コントローラーへのPUT要求をテストしようとしていますが、GETおよびPOSTボディーパラメーターをアクションに送信する必要があります。コードの特定の部分がGET paramsに明示的にアクセスするため、paramsを一緒にマッシュすることはできません。RspecがPUTアクションにGETパラメーターを渡す

どのようにGETパラメータをputアクションに渡しますか?

私はこのテストを持っている:

it "updates the account password to the provided value" do 
    put(:update, @params) 
    @account.reload.crypted_password.should == PasswordAccount.encrypt(@newpass, @account.password_salt) 
end  

私はアクションに送信されたクエリ文字列に「トークン」のparamを追加することができますどのように?

答えて

1

UPDATE デビッド:

def process_with_query(action, query_string="", parameters = nil, session = nil, flash = nil, http_method = 'GET') 
    # Sanity check for required instance variables so we can give an 
    # understandable error message. 
    %w(@routes @controller @request @response).each do |iv_name| 
    if !(instance_variable_names.include?(iv_name) || instance_variable_names.include?(iv_name.to_sym)) || instance_variable_get(iv_name).nil? 
     raise "#{iv_name} is nil: make sure you set it in your test's setup method." 
    end 
    end 

    @request.recycle! 
    @response.recycle! 
    @controller.response_body = nil 
    @controller.formats = nil 
    @controller.params = nil 

    @html_document = nil 
    @request.env['REQUEST_METHOD'] = http_method 

    parameters ||= {} 
    @request.assign_parameters(@routes, @controller.class.name.underscore.sub(/_controller$/, ''), action.to_s, parameters) 

    @request.session = ActionController::TestSession.new(session) unless session.nil? 
    @request.session["flash"] = @request.flash.update(flash || {}) 
    @request.session["flash"].sweep 

    @controller.request = @request 
    @controller.params.merge!(parameters) 
    build_request_uri(action, parameters) 
    @request.env["QUERY_STRING"] = query_string.kind_of?(Hash) ? query_string.to_param : query_string 
    @request.env["action_dispatch.request.query_parameters"] = Rack::Utils.parse_query(@request.env["QUERY_STRING"]) 
    ActionController::Base.class_eval { include ActionController::Testing } 
    @controller.process_with_new_base_test(@request, @response) 
    @request.session.delete('flash') if @request.session['flash'].blank? 
    @response 
end 

def put_with_query(action, query_string="", parameters=nil, session=nil, flash=nil) 
    process_with_query(action, query_string, parameters, session, flash, 'PUT') 
end 

def post_with_query(action, query_string="", parameters=nil, session=nil, flash=nil) 
    process_with_query(action, query_string, parameters, session, flash, 'POST') 
end 

任意のコメント:

レールのテストのソースコードを調査した後、私は、次のを思い付きましたか?

3

putメソッドは、RailsのActionController::TestCase::Behaviorで定義されています:私は別のGET /ポストのparamsを分離するためのサポートがない見ることができるものから

http://api.rubyonrails.org/classes/ActionController/TestCase/Behavior.html#method-i-put

が、なぜちょうど@paramsに含めません変数?

put(:update, @params.merge(:token => 'gobbledygook')) 

あなたのコードはのparamsを見ている時間で、Railsはすでにとにかくあなたのためのget /ポストのparamsをマージしました。

HTH、

+0

コントローラー側で別々にしておきたいと思います。私は直接いくつかのラックミドルウェアでrequest.GETとrequest.POSTにアクセスしています。 –

関連する問題