2018年4月22日 星期日
Rspec aggregate_failures syntax
意思就是在 aggregate_failures block 底下任一個的 expectation 有 error 時會針對每個 expectation 顯示 error 的 message
example
```ruby
it "returns false if any of balance, locked, saving is negative" do
negative_balance_account = build(:account, balance: -1)
negative_locked_account = build(:account, locked: -1)
negative_saving_account = build(:account, saving: -1)
aggregate_failures "testing all senarios" do
expect(negative_balance_account.has_negative_asset?).to be true
expect(negative_locked_account.has_negative_asset?).to be true
expect(negative_saving_account.has_negative_asset?).to be true
end
end
```
假設這個 test failed 了,那 console 的訊息會是像這樣:
```
1) Account#has_negative_asset? returns false if any of balance, locked, saving is negative
Got 2 failures from failure aggregation block "testing all senarios".
# ./spec/models/account_spec.rb:15:in `block (3 levels) in <top (required)>'
1.1) Failure/Error: expect(negative_locked_account.has_negative_asset?).to be true
expected true
got false
# ./spec/models/account_spec.rb:17:in `block (4 levels) in <top (required)>'
1.2) Failure/Error: expect(negative_saving_account.has_negative_asset?).to be true
expected true
got false
# ./spec/models/account_spec.rb:18:in `block (4 levels) in <top (required)>'
```
會顯示哪幾個 test failed 了
當有很多 test case 很相近其實可以寫在一個senario 裡時用 aggregate_failures 會讓 test 變得更簡潔,但要小心不要亂用,該分開的 test 還是用 it 分開比較好。
https://relishapp.com/rspec/rspec-core/docs/expectation-framework-integration/aggregating-failures
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言