Skip to content

Commit

Permalink
Refactor tests: use double for IDs as other inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-psarga committed Jan 10, 2020
1 parent 7e869f7 commit acdeca6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
5 changes: 3 additions & 2 deletions spec/cucumber/core/test/case_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ module Test
include Core
include Core::Gherkin::Writer

let(:id) { double }
let(:name) { double }
let(:location) { double }
let(:tags) { double }
let(:language) { double }
let(:test_case) { Test::Case.new('some-random-uid', name, test_steps, location, tags, language) }
let(:test_case) { Test::Case.new(id, name, test_steps, location, tags, language) }
let(:test_steps) { [double, double] }

context 'describing itself' do
Expand Down Expand Up @@ -45,7 +46,7 @@ module Test
expect( first_hook ).to receive(:describe_to).ordered.and_yield
expect( second_hook ).to receive(:describe_to).ordered.and_yield
around_hooks = [first_hook, second_hook]
Test::Case.new('some-random-uid', name, [], location, tags, language, around_hooks).describe_to(visitor, double)
Test::Case.new(id, name, [], location, tags, language, around_hooks).describe_to(visitor, double)
end

end
Expand Down
44 changes: 23 additions & 21 deletions spec/cucumber/core/test/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@
module Cucumber::Core::Test
describe Runner do

let(:step_id) { double }
let(:test_id) { double }
let(:name) { double }
let(:location) { double }
let(:tags) { double }
let(:language) { double }
let(:test_case) { Case.new('random-test-uid', name, test_steps, location, tags, language) }
let(:test_case) { Case.new(test_id, name, test_steps, location, tags, language) }
let(:text) { double }
let(:runner) { Runner.new(event_bus) }
let(:event_bus) { double.as_null_object }
let(:passing) { Step.new('some-random-uid', text, location, location).with_action {} }
let(:failing) { Step.new('some-random-uid', text, location, location).with_action { raise exception } }
let(:pending) { Step.new('some-random-uid', text, location, location).with_action { raise Result::Pending.new("TODO") } }
let(:skipping) { Step.new('some-random-uid', text, location, location).with_action { raise Result::Skipped.new } }
let(:undefined) { Step.new('some-random-uid', text, location, location) }
let(:passing) { Step.new(step_id, text, location, location).with_action {} }
let(:failing) { Step.new(step_id, text, location, location).with_action { raise exception } }
let(:pending) { Step.new(step_id, text, location, location).with_action { raise Result::Pending.new("TODO") } }
let(:skipping) { Step.new(step_id, text, location, location).with_action { raise Result::Skipped.new } }
let(:undefined) { Step.new(step_id, text, location, location) }
let(:exception) { StandardError.new('test error') }

before do
Expand Down Expand Up @@ -223,8 +225,8 @@ module Cucumber::Core::Test

context 'with multiple test cases' do
context 'when the first test case fails' do
let(:first_test_case) { Case.new('random-test-uid', name, [failing], location, tags, language) }
let(:last_test_case) { Case.new('random-test-uid', name, [passing], location, tags, language) }
let(:first_test_case) { Case.new(test_id, name, [failing], location, tags, language) }
let(:last_test_case) { Case.new(test_id, name, [passing], location, tags, language) }
let(:test_cases) { [first_test_case, last_test_case] }

it 'reports the results correctly for the following test case' do
Expand All @@ -244,9 +246,9 @@ module Cucumber::Core::Test
hook_mapping = UnskippableAction.new do |last_result|
result_spy = last_result
end
after_hook = HookStep.new('some-random-uid', text, location, hook_mapping)
failing_step = Step.new('some-random-uid', text, location).with_action { fail }
test_case = Case.new('random-test-uid', name, [failing_step, after_hook], location, tags, language)
after_hook = HookStep.new(step_id, text, location, hook_mapping)
failing_step = Step.new(step_id, text, location).with_action { fail }
test_case = Case.new(test_id, name, [failing_step, after_hook], location, tags, language)
test_case.describe_to runner
expect(result_spy).to be_failed
end
Expand All @@ -256,8 +258,8 @@ module Cucumber::Core::Test
context "with around hooks" do
it "passes normally when around hooks don't fail" do
around_hook = AroundHook.new { |block| block.call }
passing_step = Step.new('some-random-uid', text, location, location).with_action {}
test_case = Case.new('random-test-uid', name, [passing_step], location, tags, language, [around_hook])
passing_step = Step.new(step_id, text, location, location).with_action {}
test_case = Case.new(test_id, name, [passing_step], location, tags, language, [around_hook])
expect(event_bus).to receive(:test_case_finished).with(test_case, anything) do |reported_test_case, result|
expect(result).to be_passed
end
Expand All @@ -266,8 +268,8 @@ module Cucumber::Core::Test

it "gets a failed result if the Around hook fails before the test case is run" do
around_hook = AroundHook.new { |block| raise exception }
passing_step = Step.new('some-random-uid', text, location, location).with_action {}
test_case = Case.new('random-test-uid', name, [passing_step], location, tags, language, [around_hook])
passing_step = Step.new(step_id, text, location, location).with_action {}
test_case = Case.new(test_id, name, [passing_step], location, tags, language, [around_hook])
expect(event_bus).to receive(:test_case_finished).with(test_case, anything) do |reported_test_case, result|
expect(result).to be_failed
expect(result.exception).to eq exception
Expand All @@ -277,8 +279,8 @@ module Cucumber::Core::Test

it "gets a failed result if the Around hook fails after the test case is run" do
around_hook = AroundHook.new { |block| block.call; raise exception }
passing_step = Step.new('some-random-uid', text, location, location).with_action {}
test_case = Case.new('random-test-uid', name, [passing_step], location, tags, language, [around_hook])
passing_step = Step.new(step_id, text, location, location).with_action {}
test_case = Case.new(test_id, name, [passing_step], location, tags, language, [around_hook])
expect(event_bus).to receive(:test_case_finished).with(test_case, anything) do |reported_test_case, result|
expect(result).to be_failed
expect(result.exception).to eq exception
Expand All @@ -288,8 +290,8 @@ module Cucumber::Core::Test

it "fails when a step fails if the around hook works" do
around_hook = AroundHook.new { |block| block.call }
failing_step = Step.new('some-random-uid', text, location, location).with_action { raise exception }
test_case = Case.new('random-test-uid', name, [failing_step], location, tags, language, [around_hook])
failing_step = Step.new(step_id, text, location, location).with_action { raise exception }
test_case = Case.new(test_id, name, [failing_step], location, tags, language, [around_hook])
expect(event_bus).to receive(:test_case_finished).with(test_case, anything) do |reported_test_case, result|
expect(result).to be_failed
expect(result.exception).to eq exception
Expand All @@ -299,8 +301,8 @@ module Cucumber::Core::Test

it "sends after_test_step for a step interrupted by (a timeout in) the around hook" do
around_hook = AroundHook.new { |block| block.call; raise exception }
passing_step = Step.new('some-random-uid', text, location, location).with_action {}
test_case = Case.new('random-test-uid', name, [], location, tags, language, [around_hook])
passing_step = Step.new(step_id, text, location, location).with_action {}
test_case = Case.new(test_id, name, [], location, tags, language, [around_hook])
allow(runner).to receive(:running_test_step).and_return(passing_step)
expect(event_bus).to receive(:test_step_finished).with(passing_step, anything) do |reported_test_case, result|
expect(result).to be_failed
Expand Down

0 comments on commit acdeca6

Please sign in to comment.