Skip to content

Instantly share code, notes, and snippets.

@minghz
Created January 1, 2020 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minghz/99eb6346ab5ef07973c8b46f18e8d15e to your computer and use it in GitHub Desktop.
Save minghz/99eb6346ab5ef07973c8b46f18e8d15e to your computer and use it in GitHub Desktop.
Making MiniTest work like RSpec - pt1
# For a bare Minitest setup. your setup file, for example, extended_minitest.rb
require 'minitest/autorun'
class ExtendedMinitest < Minitest::Test
extend MiniTest::Spec::DSL
end
# Your test file my_test.rb
# if in a separate file, you may need to require 'extended_minitest.rb'
class MyTest < ExtendedMinitest
# Note, you can't use the "context" block. Only "it" and "describe" are available
describe 'when using it, describe, and let scoping' do
it 'should assert something' do
assert_equal 2, 1 + 1
end
describe 'a self contained test block' do
let(:my_variable) { 'my_variable' }
it 'should be able to get my_variable' do
assert_equal 'my_variable', my_variable
end
describe 'a nested describe block' do
let(:another_variable) { 'another_variable' }
it 'should find another_variable' do
assert_equal 'another_variable', another_variable
end
it 'should find my_variable' do
assert_equal 'my_variable', my_variable
end
end
it 'should not be able to access another_variable' do
refute defined? another_variable
end
end
it 'should not be able to access my_variable' do
refute defined? my_variable
end
end
describe 'when using before and after blocks' do
before do
@my_instance_variable = 'my_instance_variable'
end
after do
# whatever you need to do on teardown here. I can't think of anything
# to put in this example, because its too simple. But you get what I mean
end
it 'accesses instance variable from before block' do
assert_equal 'my_instance_variable', @my_instance_variable
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment