RSpec - 期望值

RSpec - 期望值 首页 / RSpec入门教程 / RSpec - 期望值

当您学习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中使用旧语法,则会看到警告。

链接:https://www.learnfk.comhttps://www.learnfk.com/rspec/rspec-expectations.html

来源:LearnFk无涯教程网

如,使用此代码-

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()。

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

Go语言核心36讲 -〔郝林〕

Java性能调优实战 -〔刘超〕

TensorFlow 2项目进阶实战 -〔彭靖田〕

大厂晋升指南 -〔李运华〕

说透低代码 -〔陈旭〕

说透元宇宙 -〔方军〕

Dubbo源码剖析与实战 -〔何辉〕

后端工程师的高阶面经 -〔邓明〕

给程序员的写作课 -〔高磊〕

好记忆不如烂笔头。留下您的足迹吧 :)