加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 04:26:29 所属栏目:百科 来源:网络整理
导读:我试图通过一个操作确保某些数据保持不变: 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文件开头定义的字符串. 基本
我试图通过一个操作确保某些数据保持不变:
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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读