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)

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...