RSpec:有没有'和更改',例如`而不是改变??

时间:2016-01-23 18:50:00

标签: rspec

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

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 }

那样的东西?我找不到任何东西,而且很难在Google上搜索“而不是”的内容。

3 个答案:

答案 0 :(得分:25)

不,没有and_not且没有一般否定运算符,如https://github.com/rspec/rspec-expectations/issues/493

中所述

但是,有一种机制可以定义现有匹配器的否定版本,如http://www.rubydoc.info/github/rspec/rspec-expectations/RSpec/Matchers.define_negated_matcher中所述,您可以将其与and一起使用。

https://www.relishapp.com/rspec/rspec-expectations/v/3-4/docs/compound-expectations

记录了全套复合匹配器

答案 1 :(得分:13)

如果您试图声明某些操作不应更改计数,则可以执行

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

答案 2 :(得分:1)

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

示例

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)