DEV Community

n350071🇯🇵
n350071🇯🇵

Posted on

1 1

[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

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay