RSpec Tutorial
RSpec Resources
Selected Reading
选读
- RSpec - Expectations
- RSpec - Filtering
- RSpec - Metadata
- RSpec - Helpers
- RSpec - Subjects
- RSpec - Tags
- RSpec - Hooks
- RSpec - Stubs
- RSpec - Test Doubles
- RSpec - Matchers
- RSpec - Writing Specs
- RSpec - Basic Syntax
- RSpec - Introduction
- RSpec - Home
RSpec Resources
Selected Reading
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
选读
RSpec - Stubs
RSpec - Stubs
如果你已经读过关于RSpec Doubles(aka Mocks)的一节,那么你已经看到了RSpec Stubs。 在RSpec,一种杂乱常常被称为“Stub方法”,它的一种特殊方法,即“在”现有方法或甚至不存在的方法。
这里是关于“RSpec Doubles”一节的守则。
class ClassRoom def initiapze(students) @students = students End def pst_student_names @students.map(&:name).join( , ) end end describe ClassRoom do it the pst_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.pst_student_names).to eq( John Smith,Jill Smith ) end end
举例来说,“允许()”方法提供了我们需要测试“级Room”级的方法。 在这种情况下,我们需要一个只像学生班级那样做的物体,但这个班子实际上不存在。 我们知道,学生阶级需要提供一种名称方法,我们利用允许()为姓名制造一种方法。
值得注意的是,RSpec的辛比多年来已经改变了比值。 在旧版本的RSpec中,上述方法的模糊之处将像这样界定——
student1.stub(:name).and_return( John Smith ) student2.stub(:name).and_return( Jill Smith )
让我们采用上述代码,用旧的RSpec syntax取代allow(>)条线。
class ClassRoom def initiapze(students) @students = students end def pst_student_names @students.map(&:name).join( , ) end end describe ClassRoom do it the pst_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.pst_student_names).to eq( John Smith,Jill Smith ) end end
在执行上述法典时,你将看到这一产出。
. Deprecation Warnings: Using `stub` from rspec-mocks old `:should` syntax without exppcitly enabpng the syntax is deprec ated. Use the new `:expect` syntax or exppcitly 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实例中制造方法障碍时,请你使用新许可,但我们在此提供了旧的风格,以便你看到这一点。
Advertisements