从单个操作中编写多个功能规范期望

Writing multiple feature spec expectations from a single action

所以我正在编写我的功能测试,它是这样的,请注意 [[place_holder]]

feature 'Accounts' do
  scenario 'creating an account' do
    # visit and fill form
    expect {
      click_button 'Create Account'
    }.to [[place_holder]]
    success_message = 'Your account has been successfully created.'
    expect(page).to have_content(success_message)
  end
end

现在我想以某种方式为这个区块放置 2 个期望,这些期望是

change(User, :count).by(1)
change(Account, :count).by(1)

有没有一种方法可以将这两个期望链接成一个,是的,我知道我可以在它的场景中对每个期望进行测试,但是该代码太湿了,不需要重复和功能规范开始很慢,不需要让我的测试套件变慢。

任何suggestions/alternatives都适用

从 rspec 3.1 开始,您可以使用具有块期望的复合匹配器表达式。

expect {
  click_button 'Create Account'
}.to change(User, :count).by(1).and change(Account, :count).by(1)

http://rspec.info/blog/2014/09/rspec-3-1-has-been-released/#expectations-block-matchers-can-now-be-used-in-compound-expressions

由于数据可能会在每次测试之间被清除 运行,您可以只检查绝对值,例如

expect(User.count).to eq(1)
expect(Account.count).to eq(1)

我觉得这样更易读。