是否有與RSpec中的shared_examples類似的用於Test :: Unit測試的插件/擴展?如何獲得RSpec的共享示例,如Ruby Test :: Unit中的行爲?
5
A
回答
0
Test::Unit
測試只是Ruby類,因此您可以使用與其他任何Ruby類相同的代碼重用方法。
要編寫共享示例,您可以使用一個模塊。
module SharedExamplesForAThing
def test_a_thing_does_something
...
end
end
class ThingTest < Test::Unit::TestCase
include SharedExamplesForAThing
end
2
我能夠實現共享試驗(類似於RSpec的共享實施例)使用下面的代碼:
module SharedTests
def shared_test_for(test_name, &block)
@@shared_tests ||= {}
@@shared_tests[test_name] = block
end
def shared_test(test_name, scenario, *args)
define_method "test_#{test_name}_for_#{scenario}" do
instance_exec *args, &@@shared_tests[test_name]
end
end
end
要定義和在測試::單元測試使用共享測試:
class BookTest < ActiveSupport::TestCase
extend SharedTests
shared_test_for "validate_presence" do |attr_name|
assert_false Books.new(valid_attrs.merge(attr_name => nil)).valid?
end
shared_test "validate_presence", 'foo', :foo
shared_test "validate_presence", 'bar', :bar
end
0
require 'minitest/unit'
require 'minitest/spec'
require 'minitest/autorun'
#shared tests in proc/lambda/->
basics = -> do
describe 'other tests' do
#override variables if necessary
before do
@var = false
@var3 = true
end
it 'should still make sense' do
@var.must_equal false
@var2.must_equal true
@var3.must_equal true
end
end
end
describe 'my tests' do
before do
@var = true
@var2 = true
end
it "should make sense" do
@var.must_equal true
@var2.must_equal true
end
#call shared tests here
basics.call
end
0
看看我在幾年前寫的這個要點。它仍然偉大工程:https://gist.github.com/jodosha/1560208
# adapter_test.rb
require 'test_helper'
shared_examples_for 'An Adapter' do
describe '#read' do
# ...
end
end
像這樣來使用:
# memory_test.rb
require 'test_helper'
describe Memory do
it_behaves_like 'An Adapter'
end
2
如果您正在使用的軌道(或只是active_support),使用Concern
。
require 'active_support/concern'
module SharedTests
extend ActiveSupport::Concern
included do
# This way, test name can be a string :)
test 'banana banana banana' do
assert true
end
end
end
如果您不使用active_support,只需使用Module#class_eval
即可。
這種技術建立在Andy H.的回答,他指出:
測試::單元測試只是Ruby類,所以你可以使用[普通技術]代碼重用的
但是因爲它能夠使用ActiveSupport::Testing::Declarative#test
它的好處是不會穿出你的下劃線鍵:)
相關問題
- 1. Ruby Test :: Unit中的RSpec match_array?
- 2. 如何在RSpec中編寫可重用的共享示例
- 3. RSpec的共享實例不能共享
- 4. minitest,test :: unit和rails
- 5. 如何將實例變量傳遞給RSpec共享示例
- 6. Guard + spork + Rspec問題 - 如何刪除Test :: Unit的鉤子?
- 7. 如何避免Rspec共享示例'之前定義的'警告?
- 8. 在Ruby的Test :: Unit :: TestCase中,如何覆蓋initialize方法?
- 9. 如何使用Test :: Unit
- 10. 重新定義RSpec共享示例
- 11. 如何獲得共享error.cshtml
- 12. rails rspec與devise gem共享示例
- 13. 我該如何測試Ruby的Test :: Unit的方法?
- 14. Test :: Unit應該等於RSpec should_receive
- 15. 如何獲得我的共享偏好?
- 16. 在Test :: Unit
- 17. 如何在Fastlane中獲得共享值?
- 18. Unit Test ajax failure with Q-Unit
- 19. 如何按名稱運行特定的Ruby 1.9 Test :: Unit :: TestCase子類
- 20. J Unit Test problems?
- 21. 如何設置共享示例?
- 22. 如何擴展Ruby Test :: Unit斷言以包含assert_false?
- 23. 確保teardown在Test :: Unit :: TestCase中運行?
- 24. Ruby:當我使用Test :: Unit運行測試時,爲什麼沒有點顯示?
- 25. 如何從Test :: Unit生成HTML報告?
- 26. 如何使用Test/Unit檢查RuntimeError
- 27. 在代碼中運行Ruby單元測試(Test :: Unit)
- 28. 如何獲得共享Glympse鏈接?
- 29. 來自rakefile的Test :: Unit :: TestCase?
- 30. 如何在爲Android共享提供的消息中獲得換行符