[英]In rspec-rails, How can I create a custom ordering so that I can run the specs according to my own order?
我是RSpec的新手,我想創建一個運行規格的自定義訂單,我希望首先運行最重要的規格,然后是不太重要的規格,依此類推至最不重要的規格。 Rspec已經默認全局排序為:defined,但是我想重寫它並創建我自己的自定義排序。 我已經閱讀了一份文檔(“ https://www.relishapp.com/rspec/rspec-core/docs/configuration/overriding-global-ordering ”),但無法理解如何實現它。 如果你們可以發布一些帶有解釋的示例代碼,將不勝感激。
這是我的spec_helper.rb文件: -
RSpec.configure do |config|
#config.register_ordering :global do |examples|
# model, other = examples.partition do |example|
# example.metadata[:type] == :model
# example.metadata[:type] == :controller
# end
# model + other
# end
config.register_ordering(:global) do |items|
arr = [RSpec::ExampleGroups::User]
items.sort_by &arr.method(:index)
end
=begin
config.register_ordering(:global) do |items|
items.sort_by do |group|
case group.metadata[:type]
when :feature then 3
when :controller then 2
when :model then 1
else 4
end
end
end
=end
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
例如,這是我想在運行rspec時首先執行的spec文件: - spec / models / user_spec_.rb
require 'rails_helper'
require 'spec_helper'
describe User do
describe 'must fill in user name' do
it { expect(User.new(:name => "")).not_to be_valid }
end
describe "must fill in email" do
it { expect(User.new(:email => "")).not_to be_valid }
end
describe "when email address is already taken, do not allow duplication" do
it "should not allow duplication" do
@user = User.new(name: "Example User", email: "user12@example.com")
# dup_user = @user.dup
@user.save
expect(@user.dup).not_to be_valid
end
end
end
我嘗試將這種類型的配置與數組一起使用時得到的錯誤是: -
/spec/spec_helper.rb:29:in `sort_by': comparison of Fixnum with nil
failed (ArgumentError)
from C:/RoR/rspec_demo/spec/spec_helper.rb:29:in `block (2 levels) in
<top (required)>'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rspec-core-
3.5.4/lib/rspec/core/ordering.rb:151:in `block in register_ordering'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rspec-core-
3.5.4/lib/rspec/core/ordering.rb:69:in `order'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rspec-core-
3.5.4/lib/rspec/core/world.rb:34:in `ordered_example_groups'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rspec-core-
3.5.4/lib/rspec/core/runner.rb:87:in `run'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rspec-core-
3.5.4/lib/rspec/core/runner.rb:71:in `run'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rspec-core-
3.5.4/lib/rspec/core/runner.rb:45:in `invoke'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rspec-core-
3.5.4/exe/rspec:4:in `<top (required)>'
from C:/Ruby23-x64/bin/rspec:22:in `load'
from C:/Ruby23-x64/bin/rspec:22:in `<main>'
如果我使用以下代碼覆蓋: -
config.register_ordering(:global) do |items|
items = [RSpec::ExampleGroups::User]
#items.sort_by &arr.method(:index)
end
然后spec文件運行打賭測試沒有通過返回以下錯誤: -
1) User must fill in user name
Failure/Error: super
NoMethodError:
undefined method `inspect_output' for
RSpec::ExampleGroups::User:Class
# C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rspec-core-
3.5.4/lib/rspec/core/example_group.rb:732:in `method_missing'
# C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rspec-core-
3.5.4/lib/rspec/core/example_group.rb:625:in `block in run_examples'
# C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rspec-core-
3.5.4/lib/rspec/core/example_group.rb:623:in `map'
# C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rspec-core-
3.5.4/lib/rspec/core/example_group.rb:623:in `run_examples'
# C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rspec-core-
3.5.4/lib/rspec/core/example_group.rb:589:in `run'
# C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rspec-core-
3.5.4/lib/rspec/core/runner.rb:113:in `block (3 levels) in run_specs'
# C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rspec-core-
3.5.4/lib/rspec/core/runner.rb:113:in `map'
# C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rspec-core-
3.5.4/lib/rspec/core/runner.rb:113:in `block (2 levels) in run_specs'
# C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rspec-core-
3.5.4/lib/rspec/core/configuration.rb:1835:in `with_suite_hooks'
# C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rspec-core-
3.5.4/lib/rspec/core/runner.rb:112:in `block in run_specs'
# C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rspec-core-
3.5.4/lib/rspec/core/reporter.rb:77:in `report'
# C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rspec-core-
3.5.4/lib/rspec/core/runner.rb:111:in `run_specs'
# C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rspec-core-
3.5.4/lib/rspec/core/runner.rb:87:in `run'
# C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rspec-core-
3.5.4/lib/rspec/core/runner.rb:71:in `run'
# C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rspec-core-
3.5.4/lib/rspec/core/runner.rb:45:in `invoke'
# C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rspec-core-
3.5.4/exe/rspec:4:in `<top (required)>'
# C:/Ruby23-x64/bin/rspec:22:in `load'
# C:/Ruby23-x64/bin/rspec:22:in `<main>'
#
# Showing full backtrace because every line was filtered out.
# See docs for RSpec::Configuration#backtrace_exclusion_patterns and
# RSpec::Configuration#backtrace_inclusion_patterns for more
information.
Failed examples:
rspec ./spec/models/user_spec.rb:9 # User must fill in user name
我還閱讀了關於自定義排序的討論(“ https://github.com/rspec/rspec-core/issues/547 ”),但無法理解如何實現它。
我怎樣才能實現以下目標? 或者甚至如果我必須首先執行此文件中的單個示例,首先在運行RSpec時首先執行“不應允許復制”。 我怎樣才能成功呢?
要定義自定義訂單,您只需在配置中設置自己的規則。 由於您要專門定義重要和非重要,您需要使用所需的順序創建自己的列表,然后使用它對實際文本列表進行排序。 以下是您在spec/spec_helper.rb
的原始工作示例:
config.register_ordering(:global) do |items|
arr = [RSpec::ExampleGroups::ImportantText, RSpec::ExampleGroups::NotImportant]
items.sort_by &arr.method(:index)
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.