如何将旧的 rspec 语法 "it { should_not be_valid }" 转换为 rspec2?

how to transform old rspec-syntax "it { should_not be_valid }" to rspec2?

从旧 rspec 语法转换为新语法:

旧:

 describe "when password is not present" do
    before { @user.password = @user.password_confirmation = " " }

    it { should_not be_valid }
  end

使用更新的 expect-syntax,it-part 应该是什么样子?

使用期望语法测试将是:

it { is_expected.not_to be_valid }

请注意,对于单行语法,仍支持 should-syntax(即没有弃用警告),因此您无需进行任何更改。来自 Relish page on RSpec Core 3.1:

  • is_expected is defined simply as expect(subject) and is designed for when you are using rspec-expectations with its newer expect-based syntax.
  • should was designed back when rspec-expectations only had a should-based syntax. However, it continues to be available and work even if the :should syntax is disabled (since that merely removes Object#should but this is RSpec::Core::ExampleGroup#should).