LoginSignup
2
1

More than 3 years have passed since last update.

バリデーションにwith_options onを設定している時のテスト方法

Posted at

過去のプロジェクトに後追いでテスト書いてたらwith_options on: にURIの名前空間渡して条件分岐させているクラスがあったので調べてみた。

ググれば下記の記事がすぐ出てくるからなんてことないんだけど、日本語の記事がなさそうだったので備忘録兼ねて書くことにした。

rails rspec model validation with with_options statement

RSpecの場合

sample_spec.rb
expect(sample_instance).be_valid(:condition)

be_validの引数にwith_options on:で設定した条件を渡してあげるだけ

valid?メソッドの場合

valid?メソッドの場合も引数に渡せばいいのかな?と思ってやってみたらそれでOKだった。

console.rb
> sample_instance.valid?(:condition)
=> true
2
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
2
1