3
1

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 で PowerAssert を使うもっとも簡単な方法

Last updated at Posted at 2018-06-04

次のように expect_with :test_unit を追加します。

a.rb
RSpec.configure do |config|
  config.expect_with :test_unit
end

describe do
  it do
    assert { 1 + 2 == 3 * 4 }
  end
end

実行

% rspec a.rb
F

Failures:

  1) 
     Failure/Error: assert { 1 + 2 == 3 * 4 }
     
     Test::Unit::AssertionFailedError:
           assert { 1 + 2 == 3 * 4 }
                      |   |    |
                      |   |    12
                      |   false
                      3
     # ./a.rb:7:in `block (2 levels) in <top (required)>'

Finished in 0.0131 seconds (files took 0.10485 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./a.rb:6 # 

rspec-rails を使っている場合

その場合は、悲しいことに上記のようにしても動きません。
rspec-rails 側が assert 関連を定義してカオスなことになっています。
なので rspec-rails を使っている場合は素直にこちらを使わせてもらいましょう。

RSpecでpower_assertを使うためのrspec-power_assertを作ってみた
https://qiita.com/joker1007/items/88077d6b2affeb42aa1c

Rails で rspec-power_assert を assert の名前で使う方法
https://qiita.com/akicho8/items/2bfca644fa1a84f4cacc
※ rspec-rails の assert を取り除けば test-unit の assert が使えるかもしれません (未検証)

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?