Improve exceptions usage in specs:
* Throw symbols instead of raising exceptions to test code evaluation, this will reduce "bad" usages of exceptions, symbols seems a little better for now; * Add a specific exception to be used in specs for testing behaviour when an exception is raised from a recipe.
This commit is contained in:
@@ -28,8 +28,8 @@ module Producer::Core
|
||||
|
||||
describe '#evaluate' do
|
||||
it 'evaluates its code' do
|
||||
dsl = Recipe::DSL.new { raise 'error from recipe' }
|
||||
expect { dsl.evaluate(env) }.to raise_error(RuntimeError, 'error from recipe')
|
||||
dsl = Recipe::DSL.new { throw :recipe_code }
|
||||
expect { dsl.evaluate(env) }.to throw_symbol :recipe_code
|
||||
end
|
||||
|
||||
it 'returns itself' do
|
||||
@@ -43,7 +43,7 @@ module Producer::Core
|
||||
|
||||
it 'reports the recipe file path in the error' do
|
||||
allow(env).to receive(:current_recipe) { recipe }
|
||||
expect { dsl.evaluate(env) }.to raise_error(RuntimeError) { |e|
|
||||
expect { dsl.evaluate(env) }.to raise_error(SomeErrorInRecipe) { |e|
|
||||
expect(e.backtrace.first).to match /\A#{filepath}/
|
||||
}
|
||||
end
|
||||
@@ -59,17 +59,19 @@ module Producer::Core
|
||||
subject(:dsl) { Recipe::DSL.new(&code).evaluate(env) }
|
||||
|
||||
describe '#source' do
|
||||
let(:filepath) { fixture_path_for 'recipes/error' }
|
||||
let(:filepath) { fixture_path_for 'recipes/throw' }
|
||||
let(:code) { "source '#{filepath}'" }
|
||||
subject(:dsl) { Recipe::DSL.new code }
|
||||
|
||||
it 'sources the recipe given as argument' do
|
||||
expect { dsl.evaluate(env) }.to raise_error(RuntimeError, 'error from recipe')
|
||||
expect { dsl.evaluate(env) }.to throw_symbol :recipe_code
|
||||
end
|
||||
|
||||
context 'when sourced recipe is invalid' do
|
||||
let(:filepath) { fixture_path_for 'recipes/error' }
|
||||
|
||||
it 'reports its file path in the error' do
|
||||
expect { dsl.evaluate(env) }.to raise_error(RuntimeError) { |e|
|
||||
expect { dsl.evaluate(env) }.to raise_error(SomeErrorInRecipe) { |e|
|
||||
expect(e.backtrace.first).to match /\A#{filepath}/
|
||||
}
|
||||
end
|
||||
|
@@ -6,11 +6,11 @@ module Producer::Core
|
||||
subject(:dsl) { Task::DSL.new &block }
|
||||
|
||||
describe '#evaluate' do
|
||||
let(:block) { proc { raise 'error from task' } }
|
||||
let(:block) { proc { throw :task_code } }
|
||||
|
||||
it 'evaluates its code' do
|
||||
expect { dsl.evaluate(env) }
|
||||
.to raise_error(RuntimeError, 'error from task')
|
||||
.to throw_symbol :task_code
|
||||
end
|
||||
|
||||
context 'when given block is invalid' do
|
||||
@@ -26,23 +26,23 @@ module Producer::Core
|
||||
context 'when met (block evals to true)' do
|
||||
let(:block) { proc {
|
||||
condition { true }
|
||||
raise 'error after condition'
|
||||
throw :after_condition
|
||||
} }
|
||||
|
||||
it 'evaluates all the block' do
|
||||
expect { dsl.evaluate(env) }
|
||||
.to raise_error(RuntimeError, 'error after condition')
|
||||
.to throw_symbol :after_condition
|
||||
end
|
||||
end
|
||||
|
||||
context 'when not met (block evals to false)' do
|
||||
let(:block) { proc {
|
||||
condition { false }
|
||||
raise
|
||||
throw :after_condition
|
||||
} }
|
||||
|
||||
it 'stops block evaluation' do
|
||||
expect { dsl.evaluate(env) }.not_to raise_error
|
||||
expect { dsl.evaluate(env) }.not_to throw_symbol :after_condition
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user