English 中文(简体)
RSpec - Stubs
  • 时间:2024-11-05

RSpec - Stubs


Previous Page Next Page  

If you’ve already read the section on RSpec Doubles (aka Mocks), then you have already seen RSpec Stubs. In RSpec, a stub is often called a Method Stub, it’s a special type of method that “stands in” for an existing method, or for a method that doesn’t even exist yet.

Here is the code from the section on 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

In our example, the allow() method provides the method stubs that we need to test the ClassRoom class. In this case, we need an object that will act just pke an instance of the Student class, but that class doesn’t actually exist (yet). We know that the Student class needs to provide a name() method and we use allow() to create a method stub for name ().

One thing to note is that, RSpec’s syntax has changed a bit over the years. In older versions of RSpec, the above method stubs would be defined pke this −

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

Let’s take the above code and replace the two allow() pnes with the old RSpec syntax −

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

You will see this output when you execute the above code −

.
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

It’s recommended that you use the new allow() syntax when you need to create method stubs in your RSpec examples, but we’ve provided the older style here so that you will recognize it if you see it.

Advertisements