0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Rspec】共通処理concernsのテストはStructを使うといい

Posted at

##環境
Ruby 3.0.2
Rails 6.1.4.1

##Structについて

Rspecで共通処理concernsのメソッドテストを実行する際はStructを使うとテストできる

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

concerns/name_arrangeable.rb
module NameArrangeable
  def full_name
    "#{first_name} #{last_name}"
  end
end

1. Structを使ってテスト用のクラスをつくる。moduleをincludeしておく
2. 作ったクラスをnewする
→name_arrangeable.full_nameでメソッドを呼び出して使えるようになる

spec/models/name_arrangeable_spec.rb
let!(:module_test_class) { Struct.new(:name_arrangeable) { include NameArrangeable } }
let!(:name_arrangeable) { module_test_class.new }

describe '#full_name' do
  name_arrangeable.full_name
  ...
end

##参考

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?