從before(:each)塊中獲取完整的RSpec測試名稱

[英]Getting the full RSpec test name from within a before(:each) block


RSpec allows you to get the current running test method name in a before(:each) block, by doing the following:

RSpec允許您通過執行以下操作獲取before(:each)塊中當前運行的測試方法名稱:

Spec::Runner.configure do |config|
  config.before :each do |x|
    x.method_name # returns 'should be cool'
  end
end

This is for a test like:

這是一個測試,如:

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe 'Hello world' do
  it 'should be cool' do
    # test code
  end 
end

Would it be possible to get the whole test name with what it's describing, (a.k.a. 'Hello World should be cool') in the before block?

是否有可能在前面的塊中獲得整個測試名稱(a.k.a.'Hello World應該很酷')?

7 个解决方案

#1


20  

In RSpec 2.0 you can use (I'm not sure if it is a best way but it works)

在RSpec 2.0中你可以使用(我不確定它是否是最好的方式,但它有效)

x.example.metadata[:example_group][:full_description]

As for RSpec 1.X I don't know. And that's probably what you are asking for...

至於RSpec 1.X我不知道。這可能就是你所要求的......

#2


12  

I found the answer. Turns out there used to be a method called full_description on x that would do exactly what I want, however it was deprecated. The following produces the string I want:

我找到了答案。事實證明,曾經有一種名為full_description的方法在x上完全符合我的要求,但不推薦使用。以下產生我想要的字符串:

"#{x.class.description} #{x.description}"

Reference

參考

#3


7  

With Rspec 3.3 it works like this:

使用Rspec 3.3,它的工作原理如下:

RSpec.configure do |config|
  config.before :example do |x|
    Rails.logger.debug("=== running spec example #{x.metadata[:full_description].inspect}")
  end
end

#4


6  

or you can use the methods directly:

或者你可以直接使用這些方法:

x.example.description
x.example.file_path

etc.

等等

#5


3  

With the latest rspec release as of (04/01/2014) this has changed to

隨着(04/01/2014)最新的rspec發布,這已經改為

example.metadata[:description]

Checkout https://github.com/rspec/rspec-core#metadata for more information

有關詳細信息,請訪問https://github.com/rspec/rspec-core#metadata

#6


1  

on rspec 2.12.0 "#{x.class.description} #{x.example.description}" works

關於rspec 2.12.0“#{x.class.description}#{x.example.description}”有效

#7


0  

You can get the file as well. I used this to track down issues with my poltergeist specs:

您也可以獲取該文件。我用它來追蹤我的惡作劇規范的問題:

  config.before(:each, js: true) do |s|
    md = s.example.metadata
    x = md[:example_group]
    Rails.logger.debug "==>>> #{x[:file_path]}:#{x[:line_number]} #{md[:description_args]}"
  end

Note that this is the line number of the example group (not so useful), but the description of the current example, which should help you determine which one is running.

請注意,這是示例組的行號(不太有用),但是當前示例的描述,可以幫助您確定正在運行哪個。

==>>> ./spec/features/editing_profiles_spec.rb:3 ["User Edits a Profile"]

== >>> ./pecpec/features/editing_profiles_spec.rb:3 [“用戶編輯個人資料”]


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/09/28/b6804d34d08ed0dcd06c4fde5f96799f.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com