6
6

More than 5 years have passed since last update.

[Ruby] RSpecのexpectを読んでいたら

Last updated at Posted at 2014-06-27
ruby/gems/2.1.0/gems/rspec-expectations-2.14.5/lib/rspec/expectations/syntax.rb
  def expect(*target, &target_block)
    target << target_block if block_given?
    raise ArgumentError.new("You must pass an argument or a block to #expect but not both.") unless target.size == 1
    ::RSpec::Expectations::ExpectationTarget.new(target.first)
  end

定義部分だけ見ると

def expect(*target, &target_block)

targetを配列として受け取り(*target)、かつblockも渡せる構造と思いきや、

  raise ArgumentError.new("You must pass an argument or a block to #expect but not both.") unless target.size == 1

結局target.size == 1以外はエラーを吐く。

例外メッセージを明示的に設定したかったから?

こんな書き方もあるのかと勉強になりました。

6
6
1

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
6
6