多個not_to改革的期望,我想,以確保一定的數據具有通過一個單一的動作保持不變:預計rspec的
expect {
# running migration and user reload here
}.not_to change(user, :avatar_url).from(sample_avatar_url).and change(user, :old_avatar).from(nil)
sample_avatar_url
是在規範文件的開頭定義的字符串。
基本上,我想檢查avatar_url
和old_avatar
是否仍然未被expect
塊中發生的情況觸發。
從上面的代碼的輸出是:
expect(...).not_to matcher.and matcher
不被支持,因爲它產生一個比特的模糊性。相反,定義否定版本的任何匹配你想否定與RSpec::Matchers.define_negated_matcher
和使用expect(...).to matcher.and matcher
。
與第一例子簡化它的伎倆。謝謝! – abpetkov
不客氣 - 只是注意,檢查略少一點,因爲它只檢查是否設置了特定的值,而不是它從其他值改變爲該值 - 如果這足以對您的用例進行測試,那麼它的效果很好。 –
在rails_helper.rb中放置'RSpec :: Matchers.define_negated_matcher(:not_change,:change)'給了我的RSpec superpowers。 – Epigene