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

rspec 基本まとめ マッチャ 伊藤さんの記事より抜粋

Posted at

出てくるメソッド

  • be_xxx

    • be_valid
    • be_empty
    • be_falsey,be_truthy
  • change,by

# popは配列から要素を削除
# {}でかこう
x = [1, 2, 3]
expect{ x.pop }.to change{ x.size }.from(3).to(2)


# updateするときに`change`

it 'userを削除すると、userが書いたblogも削除されること' do
  user = User.create(name: 'Tom', email: 'tom@example.com')
  # user が blog を書いたことにする
  user.blogs.create(title: 'RSpec必勝法', content: 'あとで書く')

  expect{ user.destroy }.to change{ Blog.count }.by(-1)
end
  • raise_error
class ShoppingCart
  def initialize
    @items = []
  end
  def add(item)
    raise 'Item is nil.' if item.nil?
    @items << item
  end
end
it 'nilを追加するとエラーが発生すること' do
  cart = ShoppingCart.new
  expect{ cart.add nil }.to raise_error 'Item is nil.'
end

参考記事

伊藤淳一さんの記事
https://qiita.com/jnchito/items/2e79a1abe7cd8214caa5

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?