9/9-Rails-Rspec


Specファイルを確認
  1. rails_helper
  2. rails_helperとspec_helperの違い
  3. shared_example_for
  4. shared_example_forの構文
  5. assign_attributes

■1
rspecはrails_helperに設定値を書き込んで使用するファイル
ファイルの頭に、以下のように書き込んで使用可能とする。
require 'rails_helper'

■2

  • spec/rails_helper.rb に Rails 特有の設定を書く
  • spec/spec_helper.rbには RSpec の全体的な設定を書く、
というようにお作法が変わる。
これによって、Railsを必要としないテストを書きやすくなる。
(参考)Rspec3時代の設定ファイルrails_helper.rb

■3
shared_example_forは共通するcontextやitをまとめる働きをする
specでのリファクタリングをした書き方
(参考)Shared example group

■4
shared_exampleの構文は
shared_example_for 'title' do |paramater|
  let(:symbol) {described_class}

  • 'title'には、以下で処理するタイトルを記述する。クラス名を書いたりする。
  • do |paramater|には、引数を渡すことで処理をまとめることができる。
  • let(:symbol){described_class}ではインスタンス変数を生成している。
  • described_classは、クラス名を暗黙的にとれる。べた書きでクラス名を指定するより洗練しているし、typoがないことがメリットである。

(参考)
Passing parameters to a shared example group
rspecで共通するcontextやitをshared_examples_forでまとめる
RSpecのletを使うのはどんなときか?(翻訳)

■5
assign_attributesはmodelsにてobjectの属性をまとめて変更したいときに使う



rspec
Header
https://stackoverrun.com/ja/q/12428102

http1api
https://qiita.com/geshi/items/908f7a09b7c962956260


Next Post Previous Post
No Comment
Add Comment
comment url