RSpec – 钩子

RSpec – 钩子


在编写单元测试时,在测试前后运行设置和拆卸代码通常很方便。设置代码是为测试配置或“设置”条件的代码。拆解代码进行清理,确保环境处于一致状态以进行后续测试。

一般来说,你的测试应该是相互独立的。当您运行一整套测试并且其中一个失败时,您希望确信它失败是因为它正在测试的代码有错误,而不是因为之前的测试使环境处于不一致的状态。

RSpec 中最常用的钩子是前后钩子。它们提供了一种定义和运行我们上面讨论的设置和拆卸代码的方法。让我们考虑这个示例代码 –

class SimpleClass 
   attr_accessor :message 
   
   def initialize() 
      puts "\nCreating a new instance of the SimpleClass class" 
      @message = 'howdy' 
   end 
   
   def update_message(new_message) 
      @message = new_message 
   end 
end 

describe SimpleClass do 
   before(:each) do 
      @simple_class = SimpleClass.new 
   end 
   
   it 'should have an initial message' do 
      expect(@simple_class).to_not be_nil
      @simple_class.message = 'Something else. . .' 
   end 
   
   it 'should be able to change its message' do
      @simple_class.update_message('a new message')
      expect(@simple_class.message).to_not be 'howdy' 
   end
end

运行此代码时,您将获得以下输出 –

Creating a new instance of the SimpleClass class 
. 
Creating a new instance of the SimpleClass class 
. 
Finished in 0.003 seconds (files took 0.11401 seconds to load) 
2 examples, 0 failures

让我们仔细看看发生了什么。before(:each) 方法是我们定义设置代码的地方。当您传递 :each 参数时,您是在指示 before 方法在示例组中的每个示例之前运行,即上面代码中描述块内的两个 it 块。

在行中:@simple_class = SimpleClass.new,我们正在创建 SimpleClass 类的新实例并将其分配给对象的实例变量。您可能想知道什么对象?RSpec 在 describe 块的范围内在幕后创建了一个特殊的类。这允许您为此类的实例变量分配值,您可以在示例中的 it 块中访问这些变量。这也使得在我们的测试中编写更清晰的代码变得容易。如果每个测试(示例)都需要 SimpleClass 的实例,我们可以将该代码放在 before 钩子中,而不必将其添加到每个示例中。

请注意,“Creating a new instance of the SimpleClass class”行被写入控制台两次,这表明,在每个it 块中调用 hook 之前

正如我们所提到的,RSpec 也有一个 after 钩子,并且 before 和 after 钩子都可以接受: all 作为参数。after 钩子将在指定的目标之后运行。The: all target 意味着钩子将在所有示例之前/之后运行。这是一个简单的示例,说明何时调用每个钩子。

describe "Before and after hooks" do 
   before(:each) do 
      puts "Runs before each Example" 
   end 
   
   after(:each) do 
      puts "Runs after each Example" 
   end 
   
   before(:all) do 
      puts "Runs before all Examples" 
   end 
   
   after(:all) do 
      puts "Runs after all Examples"
   end 
   
   it 'is the first Example in this spec file' do 
      puts 'Running the first Example' 
   end 
   
   it 'is the second Example in this spec file' do 
      puts 'Running the second Example' 
   end 
end

当你运行上面的代码时,你会看到这个输出 –

Runs before all Examples 
Runs before each Example 
Running the first Example 
Runs after each Example 
.Runs before each Example 
Running the second Example 
Runs after each Example 
.Runs after all Examples

觉得文章有用?

点个广告表达一下你的爱意吧 !😁