0
0

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

【API実装・下書き機能】rspec テスト model, requestsの違い

Last updated at Posted at 2021-03-16

create, build でテストできることの違い

「下書きが保存可能であること」を確認しようと思い、
下記のコードを記述。

  context "下書きのとき" do
    let(:draft) { create(:article, status: 0) }

    fit "下書き記事を保存できる" do
      expect(draft).to be_valid
    end
  end 

しかし、これでは、
createした後に、createできるか確認。
という順になってしまうため、保存できるかの確認にはならない。(createは作ってすぐに「保存してしまっている」から。)

なので、create -> build に変えるのが正しい。

  context "下書きのとき" do
    let(:draft) { build(:article, status: 0) }

    fit "下書き記事を保存できる" do
      expect(draft).to be_valid
    end
  end 

これなら、draftは保存可能なデータなのか?
というテストになる。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?