0
0

Rspecを始めて行う

Posted at
bundle exec rspec spec/models

このディレクトリ以下のテストを実行する

RSpec.describe User, type: :model do
  context "with 2 or more comments" do
    it "orders them in reverse chronologically" do
      user = User.create!(:name => "hogehoge", :email => "gehogeho",  :forign => 1)
     expect(user).to eq(user)
    end
  end
end

テストのコメントは出鱈目です。(後でやります)

expect(user).to eq(user)

In RSpec, assertions are called expectations, and every expectation is built around a matcher. When you expect(a).to eq(b), you’re using the eq matcher.

出典

https://rspec.info/documentation/6.1/rspec-rails/#:~:text=In%20RSpec%2C%20assertions%20are%20called%20expectations%2C%20and%20every%20expectation%20is%20built%20around%20a%20matcher.%20When%20you%20expect(a).to%20eq(b)%2C%20you%E2%80%99re%20using%20the%20eq%20matcher.

自分の訳

Rspecにおいて断言は期待と呼ばれる。そして全ての期待はマッチを中心に建てられる。あなたが期待値aでマッチはbの時、あなたはeqマッチを使用する

とりあえず

RSpec.describe Widget, type: :model do
  it "has none to begin with" do
    expect(Widget.count).to eq 0
  end

出典

とりあえず
eqの値と一致すればテストが通るらしい。

その他のマッチャー

感想

生成したモデル同士を比べているから同じなのは当たり前だがテストを通ることができたので良しとする。

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