LoginSignup
0
0

More than 3 years have passed since last update.

[Rails5] Rspec -ネスト時の単体テスト-

Posted at

・はじめに

Routingネスト時の単体テストについて備忘録として残します。

1. 前提条件

まず以下の様にpostテーブルが存在しているとします、

create_table "posts", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
    t.string "content", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "group_id"
    t.integer "user_id"
  end

そして以下の様にgroup配下にネストされている状態です。

  resources :groups do
    resources :posts, only: %i[new create edit update]
  end

2.結論

そこまで難しいことでは無いので結論を書きます。

上記の様な条件ならば

期待する動作に親モデルidのパラメーターを渡してあげるだけでした
(私はわざわざ親モデルを直接作成しようとして大ハマりしました)

  before do
    get :new, params: { group_id: group.id }
  end

すごくシンプルにハッシュ形式で覚える事が出来ました。

0
0
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
0