DEV Community

n350071🇯🇵
n350071🇯🇵

Posted on

[RSpec] for ApplicationController

TL;DR

Use controller.send(:method_name, arg)

🤔 Situation

class ApplicationController < ActionController::Base
  def auto_encode(target)
    UserAgentBrowser.new(request.user_agent).os =~ /Windows/ ? URI.escape(target) : target
  end
end

👍 You can write like this

require 'rails_helper'

describe V3::ApplicationController, type: :controller do
  describe '#auto_encode' do
    let(:text) { 'にほんご' }
    let(:encoded_text) { '%E3%81%AB%E3%81%BB%E3%82%93%E3%81%94' }
    context 'user_agent is chrome' do
      it { expect(controller.send(:auto_encode, text)).to eq text }
    end
    context 'user_agent is not chrome' do
      let(:user_agent) { 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.3;)' }
      before { request.user_agent = user_agent }
      it { expect(controller.send(:auto_encode, text)).to eq encoded_text }
    end
  end
end

🔗 Parent Note

Top comments (0)