ruby-on-rails – 期望在rspec中多次改变期望值

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 期望在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是在spec文件开头定义的字符串.

基本上,我想检查avatar_url和old_avatar是否保持不受期望块中发生的事情的影响.

上面代码输出是:

expect(...).not_to matcher.and matcher is not supported,since it creates a bit of an ambiguity. Instead,define negated versions of whatever matchers you wish to negate with RSpec::Matchers.define_negated_matcher and use expect(...).to matcher.and matcher.

解决方法

这不起作用,因为它不清楚读取是否应该意味着不改变第一个而不是改变第二个,或者不改变第一个但改变第二个.你有几个选择来解决这个问题

由于您正在检查静态值,因此不要使用更改

..run migration and user reload..
expect(user.avatar_url).to eq(sample_avatar_url)
expect(user.old_avatar).to eq nil

或使用define_negated_matcher创建not_change匹配器

RSpec::Matchers.define_negated_matcher :not_change,:change
expect {
  # running migration and user reload here
}.to not_change(user,:avatar_url).from(sample_avatar_url).and not_change(user,:old_avatar).from(nil)
原文链接:https://www.f2er.com/ruby/268915.html

猜你在找的Ruby相关文章