1
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 5 years have passed since last update.

rspec : subjectがなくてもテストは通る。その場合の書き方。

1
Last updated at Posted at 2019-08-04

subjectを使った場合

spec/requests/articles_spec.rb
require 'rails_helper'

RSpec.describe "Articles", type: :request do
  describe "GET /api/v1/articles" do
    subject { get(api_v1_articles_path) }  ここでリクエストのpathをsubjectで指定こうすることで subject のみの記述で同じリクエストを指定できるドライに書ける

    let(:article_count) { 5 }
    before do
      create(:user, :with_articles, article_count: article_count)
    end

    it "articlesの一覧を取得できる" do
      subject  ←このsubject一行で上で指定したpathを同じ意味になる(同じpathにリクエストを送る)
      res = JSON.parse(response.body).count
      expect(res).to eq article_count
    end

  end
end

subjectを使わない場合

spec/requests/articles_spec.rb
require 'rails_helper'

RSpec.describe "Articles", type: :request do
  describe "GET /api/v1/articles" do

    let(:article_count) { 5 }
    before do
      create(:user, :with_articles, article_count: article_count)
    end

    it "articlesの一覧を取得できる" do
      get(api_v1_articles_path)   ←subjectを使わずにpathの指定
      res = JSON.parse(response.body).count
      expect(res).to eq article_count
    end

  end
end

参考
https://qiita.com/takutotacos/items/f65b47d18c124110eebd

1
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
1
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?