🖥

#Rails + #rspec でハッシュに対して一部のkey/valueの型だけを検証する、ゆるいテストをするには include と b

2019/12/14に公開
expect(a: 1, b: Time.now, c: 'wow').to include({a: be_a(Integer)})
# => true

expect(a: 1, b: Time.now, c: 'wow').to include({b: be_a(Time)})
# => true

expect(a: 1, b: Time.now, c: 'wow').to include({c: be_a(String)})
# => true

expect(a: 1, b: Time.now, c: 'wow').to include({c: be_a(Integer)})
# => [#<RSpec::Expectations::ExpectationNotMetError: expected {:a => 1, :b => 2019-12-13 02:08:55.283922100 +0000, :c => "wow"} to include {:c => (be a kind of Integer)}
# Diff:
# @@ -1,2 +1,4 @@
# -:c => (be a kind of Integer),
# +:a => 1,
# +:b => 2019-12-13 02:08:55.283922100 +0000,
# +:c => "wow",
# >]

ref

ruby - Testing hash contents using RSpec - Stack Overflow
https://stackoverflow.com/questions/8392884/testing-hash-contents-using-rspec

Original by Github issue

https://github.com/YumaInaura/YumaInaura/issues/2845

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

Twitter

https://twitter.com/YumaInaura

公開日時

2019-12-14

Discussion