首页 > 其他分享 >无涯教程-RSpec - Stubs

无涯教程-RSpec - Stubs

时间:2023-11-19 10:32:18浏览次数:108  
标签:end name students Stubs Smith 无涯 RSpec student

如果您已经阅读了RSpec Doubles部分,那么您已经看到了RSpec Stubs,它是一种特殊类型的方法,代表现有方法或尚不存在的方法。

这是RSpec Doubles部分中的代码-

class ClassRoom 
   def initialize(students) 
      @students=students 
   End
   
   def list_student_names 
      @students.map(&:name).join(',') 
   end 
end 

describe ClassRoom do 
   it 'the list_student_names method should work correctly' do 
      student1=double('student') 
      student2=double('student') 
      
      allow(student1).to receive(:name) { 'John Smith'}
      allow(student2).to receive(:name) { 'Jill Smith'} 
      
      cr=ClassRoom.new [student1,student2]
      expect(cr.list_student_names).to eq('John Smith,Jill Smith') 
   end 
end

在无涯教程的示例中,allow()方法提供了测试ClassRoom类所需的方法。在这种情况下,需要一个对象,其行为类似于Student类,但是该类实际上并不存在。知道Student类需要提供一个name()方法,并且使用allow()为name()创建一个方法。

需要注意的一件事是,多年来RSpec的语法发生了一些变化。在较旧的RSpec版本中,上述方法存根的定义如下:

student1.stub(:name).and_return('John Smith') 
student2.stub(:name).and_return('Jill Smith')

采用上面的代码,并用旧的RSpec语法替换两条 allow()行-

class ClassRoom 
   def initialize(students) 
      @students=students 
   end 
   
   def list_student_names 
      @students.map(&:name).join(',') 
   end 
	
end 

describe ClassRoom do 
   it 'the list_student_names method should work correctly' do 
      student1=double('student') 
      student2=double('student')
      
      student1.stub(:name).and_return('John Smith')
      student2.stub(:name).and_return('Jill Smith') 
      
      cr=ClassRoom.new [student1,student2] 
      expect(cr.list_student_names).to eq('John Smith,Jill Smith') 
   end 
end

执行上面的代码时,您将看到此输出-

.
Deprecation Warnings:

Using `stub` from rspec-mocks' old `:should` syntax without explicitly 
   enabling the syntax is deprec 

ated. Use the new `:expect` syntax or explicitly enable `:should` instead. 
   Called from C:/rspec_tuto 

rial/spec/double_spec.rb:15: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.002 seconds (files took 0.11401 seconds to load)
1 example, 0 failures

建议您在RSpec示例中创建方法Stubs时使用新的allow()语法。

参考链接

https://www.learnfk.com/rspec/rspec-stubs.html

标签:end,name,students,Stubs,Smith,无涯,RSpec,student
From: https://blog.51cto.com/u_14033984/8470363

相关文章

  • 无涯教程-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......
  • 无涯教程-D语言 - 类和对象
    类(Class)类可以看成是创建Java对象的模板,中的数据和函数称为该类的成员。类定义类定义以关键字class开头,后跟类名,类定义之后必须是分号或声明列表,如,我们使用关键字class定义Box数据类型,如下所示-classBox{public:doublelength;//box的长度doubl......
  • 无涯教程-D语言 - 异常处理
    Exception异常是在程序执行期间出现的问题,异常提供了一种将控制权从程序的一部分转移到另一部分的方法。D异常处理基于三个关键字try,catch和throw。throw   - 出现问题时,程序将引发异常。这是通过throw关键字完成的。catch   -  catch关键字用于捕获......
  • 无涯教程-D语言 - 并发
    并发使程序在多个线程上运行,一个示例是Web服务器同时响应多个客户端,并发通过消息传递很容易,但是它们基于数据共享则很难编写。启动线程函数spawn()将指针作为参数,并从该函数启动新线程,该函数执行的任何操作,包括它可能调用的其他函数,都将在新线程上执行。importstd.stdio;im......
  • 无涯教程-D语言 - 文件I/O
    文件File由std.stdio模块的File结构表示,文件表示字节序列,无论是文本文件还是二进制文件都没有关系,D编程语言提供对高级函数的访问/处理存储设备上文件。打开文件首先通过指定文件名和所需的访问权限来打开文件。Filefile=File(filepath,"mode");在这里,filename是字符串......
  • 无涯教程-D语言 - 不可变(Immutables)
    我们经常使用可变的变量,但是在很多情况下不需要可变性。D的不变性概念由const和immutable关键字表示,尽管这两个词本身的含义很接近,但它们在程序中的职责有所不同,有时是不兼容的。枚举常量枚举常量使将常量值与有意义的名称相关联成为可能,一个简单的如下所示。importstd.stdi......