以下の内容はhttps://shinkufencer.hateblo.jp/entry/2019/01/15/233000より取得しました。


RSpec内でcontrollerを定義して、concernのメソッドをテストする

俗に言うAnonymousControllerという手法です。

環境

$ bundle exec rspec --version
RSpec 3.8
  - rspec-core 3.8.0
  - rspec-expectations 3.8.2
  - rspec-mocks 3.8.0
  - rspec-rails 3.8.1
  - rspec-support 3.8.0

書き方

下記のようなconcernがあったとする

module Targetable
  extend ActiveSupport::Concern

  def public_na_method
    true
  end
end

publicメソッドを試したい場合は下記のように書く(helperは環境に応じて適宜書き足してください)

RSpec.describe "Targetable", type: :controller do
  controller ApplicationController do
    include Targetable
  end
  
  describe "#public_na_method" do
    it "動くこと" do
      expect(controller.public_na_method).to eq(true)
    end
  end
end

参考




以上の内容はhttps://shinkufencer.hateblo.jp/entry/2019/01/15/233000より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14