当您学习RSpec时,您可能会读到很多关于期望值(Expectations)的内容,起初可能会有些混乱。当您看到"Expectations"一词时,应牢记两个主要细节-
Expectations 只是 it块中使用 expect()方法的一条语句。当您具有如下代码: expect(1 + 1).to eq(2)时,您期望表达式 1 + 1 的值为 2 。
Expectation 语法是相对较新的。在引入 expect()方法之前(早在2012年),RSpec使用基于 should()方法的另一种语法。上面的期望是用旧语法这样写的:(1 + 1).should eq(2)。
使用较旧的基于代码的版本或较旧版本的RSpec时,可能会遇到旧RSpec语法,如果在新版本的RSpec中使用旧语法,则会看到警告。
如,使用此代码-
RSpec.describe "An RSpec file that uses the old syntax" do it 'you should see a warning when you run this Example' do (1 + 1).should eq(2) end end
运行它时,您将获得如下所示的输出:
. Deprecation Warnings: Using `should` from rspec-expectations' old `:should` syntax without explicitly enabling the syntax is deprecated. Use the new `:expect` syntax or explicitly enable `:should` with `config.expect_with( :rspec) { |c| c.syntax=:should }` instead. Called from C:/rspec_tutorial/spec/old_expectation.rb:3 :in `block (2 levels) in <top (required)>'. If you need more of the backtrace for any of these deprecations to identify where to make the necessary changes, you can configure `config.raise_errors_for_deprecations!`, and it will turn the deprecation warnings into errors, giving you the full backtrace. 1 deprecation warning total Finished in 0.001 seconds (files took 0.11201 seconds to load) 1 example, 0 failures
除非需要使用旧的语法,否则强烈建议您使用Expect()而不是should()。
参考链接
https://www.learnfk.com/rspec/rspec-expectations.html
标签:无涯,RSpec,syntax,语法,rspec,expect,should,期望值 From: https://blog.51cto.com/u_14033984/8473813