RSpec:是否有一个不代表“和改变”的词,例如“和不改变”?

RSpec:是否有一个不代表“和改变”的词,例如“和不改变”?,rspec,Rspec,我发现和方法对于链接许多期望非常有用 expect { click_button 'Update Boilerplate' @boilerplate_original.reload } .to change { @boilerplate_original.title }.to('A new boilerplate') .and change { @boilerplate_original.intro }.to('Some nice introduction') 有什么东西让我检查

我发现
方法对于链接许多期望非常有用

expect {
  click_button 'Update Boilerplate'
  @boilerplate_original.reload
} .to  change { @boilerplate_original.title }.to('A new boilerplate')
  .and change { @boilerplate_original.intro }.to('Some nice introduction')
有什么东西让我检查一下是否没有变化吗

.and_not change { @boilerplate_original.intro }

像这样的?我找不到任何东西,而且很难在谷歌上搜索像“and not”这样的词。

不,没有
和\u not
以及一般的否定运算符,如中所述

但是,有一种机制可以定义现有匹配器的否定版本,如中所述,可与
一起使用


完整的复合匹配器集记录在

中。如果您试图断言某些操作不应更改计数,您可以这样做

expect { something }.to change { Foo.count }.by(1).and change { Bar.count }.by(0)

您可以通过
RSpec::Matchers定义否定匹配器。定义否定匹配器

范例

RSpec::Matchers.define_negated_matcher :not_include, :include
RSpec::Matchers.define_negated_matcher :not_eq, :eq
将这一行置于任何上下文之外的文件开头,放入另一个文件并将该文件加载到测试中

现在你可以写了

expect([1, 2, 3]).to include(1).and not_include(5).and not_include(6)
expect(100).to eq(100).and not_eq(200)

太长,读不下去了将
RSpec::Matchers.define\u否定\u matcher:not\u change,:change
放入
spec\u helper.rb