首页 > 其他分享 >无涯教程-RSpec - 期望值

无涯教程-RSpec - 期望值

时间:2023-11-19 15:31:48浏览次数:29  
标签:无涯 RSpec syntax 语法 rspec expect should 期望值

当您学习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

相关文章

  • 无涯教程-RSpec - 过滤器
    在阅读本节之前,您可能需要阅读有关RSpec元数据(Metadata)的部分,因为事实证明,RSpec筛选(Filtering)基于RSpec元数据。假设您有一个spec文件,它包含两种类型的测试:正确测试和错误测试。让无涯教程这样定义它们-RSpec.describe"AnExampleGroupwithpositiveandnegativeExam......
  • 无涯教程-RSpec - 元数据
    RSpec是一种灵活而强大的工具。RSpec中的元数据(Metadata)函数也不例外。元数据通常指“关于数据的数据”。在RSpec中,这意味着有关您的描述,上下文及其块的数据,让无涯教程看一个例子-RSpec.describe"AnExampleGroupwithametadatavariable",:foo=>17docontext'an......
  • 无涯教程-RSpec - Subjects主题
    RSpecsubjets提供了编写简写测试用例的快捷方式。考虑以下代码-classPersonattr_reader:first_name,:last_namedefinitialize(first_name,last_name)@first_name=first_name@last_name=last_nameendenddescribePersondo......
  • 无涯教程-RSpec - Tags标签
    RSpecTags提供了一种在规范文件中运行特定测试的简便方法。假设您只想运行指定测试方法,则可以使用Tags标签实现。describe"HowtorunspecificExampleswithTags"doit'isaslowtest',:slow=>truedosleep10puts'Thistestisslow!'end......
  • 无涯教程-RSpec - Stubs
    如果您已经阅读了RSpecDoubles部分,那么您已经看到了RSpecStubs,它是一种特殊类型的方法,代表现有方法或尚不存在的方法。这是RSpecDoubles部分中的代码-classClassRoomdefinitialize(students)@students=studentsEnddeflist_student_names......
  • 无涯教程-RSpec - 模拟对象
    在本章中,无涯教程将讨论RSpecDoubles,RSpecDouble是一个模拟对象,在代码中模拟系统的另一个对象,方便测试。假设您为学校构建一个应用程序,有一个教室,还有一个学生。类定义如下:classClassRoomdefinitialize(students)@students=studentsenddeflis......
  • 无涯教程-RSpec - 基本语法
    让无涯教程仔细看看HelloWorld示例的代码。首先,如果不清楚,正在测试HelloWorld类的函数。当然,这是一个非常简单的类,仅包含一个方法say_hello()。这又是RSpec代码-describeHelloWorlddocontext“WhentestingtheHelloWorldclass”doit"Thesay_......
  • 无涯教程-RSpec - 简介
    行为驱动开发(英语:Behavior-drivendevelopment,缩写BDD)是一种敏捷软件开发的技术,它鼓励软件项目中的开发者、QA和非技术人员或商业参与者之间的协作。BDD最初是由DanNorth在2003年命名,它包括验收测试和客户测试驱动等的极限编程的实践,作为对测试驱动开发的回应。在过去数年里,它得......
  • 无涯教程-D语言 - 封装
    封装是一种面向对象的编程概念,它将数据和将数据操作在一起的函数绑定在一起,并且可以确保不受外界干扰,封装导致了数据隐藏的重要OOP概念。一个类可以包含private,protected和public修饰符,默认情况下,类中定义的所有项目都是private私有的。如-classBox{public:......
  • 无涯教程-D语言 - 继承
    面向对象编程中最重要的概念之一是继承,继承允许使用一个类继承另一个类,这样就可以直接调用父类的公共函数或变量,这使得维护变得更加容易。基类和子类子类通过":"冒号来实现继承基类。classderived-class:base-class考虑如下基类Shape及其派生类Rectangle-importstd.s......