Module: Megatest::DSL
- Included in:
- Test
- Defined in:
- lib/megatest/dsl.rb
Overview
All the methods necessary to define test cases. Can be used directly to define test cases in modules for later inclusion.
Example:
module SomeSharedTests
extend Megatest::DSL
setup do
end
test "the truth" do
assert_equal 4, 2 + 2
end
end
class SomeTest < Megatest::Test
include SomeSharedTests
end
class SomeOtherTest < Megatest::Test
include SomeSharedTests
end
Class Method Summary collapse
Instance Method Summary collapse
-
#around(&block) ⇒ Object
Registers a block to be invoked around every test cases.
-
#context(name, tags = nil, &block) ⇒ Object
Creates a context block, for logically grouping test cases.
- #method_added(name) ⇒ Object
-
#setup(*methods, &block) ⇒ Object
Registers a block to be invoked before every test cases.
-
#tag(**kwargs) ⇒ Object
Applies tags to all the test case of this suite.
-
#teardown(*methods, &block) ⇒ Object
Registers a block to be invoked after every test cases, regardless of whether it passed or failed.
-
#test(name, tags = nil, &block) ⇒ Object
Define a test case.
Class Method Details
Instance Method Details
#around(&block) ⇒ Object
Registers a block to be invoked around every test cases. The block will receive a Proc as first argument and MUST call it.
Example:
around do |block|
do_something do
block.call
end
end
160 161 162 |
# File 'lib/megatest/dsl.rb', line 160 def around(&block) ::Megatest.registry.suite(self).on_around(block) end |
#context(name, tags = nil, &block) ⇒ Object
Creates a context block, for logically grouping test cases. The context string will be prepended to all the test cases defined within the block.
Example:
context "maths" do
test "the truth" do
assert_equal 4, 2 + 2
end
test "oddity" do
refute_predicate 4, odd?
end
end
Setup and teardown callbacks are not allowed within a context blocks, as it too easily lead to “write only” tests. It’s only meant to help group test cases together.
If you need a common setup procedure, just define a helper method, and explicitly call it.
Example:
context "admin user" do
def setup_admin_user
# ...
end
test "#admin?" do
user = setup_admin_user
assert_predicate user, :admin?
end
test "#can?(:delete_post)" do
user = setup_admin_user
assert user.can?(:delete_post)
end
end
134 135 136 |
# File 'lib/megatest/dsl.rb', line 134 def context(name, = nil, &block) ::Megatest.registry.suite(self).with_context(name, , &block) end |
#method_added(name) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/megatest/dsl.rb', line 45 def method_added(name) super if name.start_with?("test_") ::Megatest.registry.suite(self).register_test_case(name, instance_method(name), nil) end end |
#setup(*methods, &block) ⇒ Object
Registers a block to be invoked before every test cases.
139 140 141 142 143 144 145 146 147 |
# File 'lib/megatest/dsl.rb', line 139 def setup(*methods, &block) suite = ::Megatest.registry.suite(self) methods.each do |m| suite.on_setup(-> { send(m) }) end if block suite.on_setup(block) end end |
#tag(**kwargs) ⇒ Object
90 91 92 |
# File 'lib/megatest/dsl.rb', line 90 def tag(**kwargs) ::Megatest.registry.suite(self).(kwargs) end |
#teardown(*methods, &block) ⇒ Object
Registers a block to be invoked after every test cases, regardless of whether it passed or failed.
166 167 168 169 170 171 172 173 174 |
# File 'lib/megatest/dsl.rb', line 166 def teardown(*methods, &block) suite = ::Megatest.registry.suite(self) methods.each do |m| suite.on_teardown(-> { send(m) }) end if block suite.on_teardown(block) end end |
#test(name, tags = nil, &block) ⇒ Object
Define a test case.
Example:
test "the truth" do
assert_equal 4, 2 + 2
end
For ease of transition from other test frameworks, any method that starts by test_ is also considered a test:
Example:
def test_the_truth
assert_equal 4, 2 + 2
end
71 72 73 |
# File 'lib/megatest/dsl.rb', line 71 def test(name, = nil, &block) ::Megatest.registry.suite(self).register_test_case(-name, block, ) end |