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