LoginSignup
10
11

More than 3 years have passed since last update.

RSpecのassignsメソッドを復習するぞ

Last updated at Posted at 2020-07-07

assignsメソッドの使い方

assignsメソッドはコントローラのインスタンス変数をテストするメソッド。
引数にインスタンス変数をシンボル型で渡す。(超重要)

categories_controller_spec.rb
require 'rails_helper'

RSpec.describe Potepan::CategoriesController, type: :controller do

  describe "GET #show" do
    let!(:taxon) { create(:taxon, name: "Bags") }
    before { get :show, params: { id: taxon.id } }

    it "assigns @taxon" do
      expect(assigns(:taxon)).to eq taxon
    end
  end
end

この場合、コントローラ側で@taxonというインスタンス変数を定義していることになる。

テストの内容としては、コントローラで作成した@taxonというインスタンス変数とテスト内で作成したtaxonというテストデータが一致しているかを検証している。

10
11
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
10
11