Week 1, day 6

So yesterday we started our weekend challenge. Being able to apply our knowledge to a similar yet different real-world challenge so quickly is a great learning experience. The plan of attack was to use the user-stories to come up with an outside-in, behaviour-driven design approach to coding, feature tests first, then unit tests, then watch them fail, then create the necessary application code to make them pass.

 1 require 'capybara/rspec'
 2 require 'airport'
 3 feature 'grand finale' do
 4   scenario 'all planes can land and all planes can take off' do
 5     airport = Airport.new
 6     planes = []
 7     6.times { planes << Plane.new  }
 8     planes.each { |plane| airport.landing_permission plane }
 9     expect(airport.landed_planes.length).to eq 6
10     airport.landed_planes.each do |plane|
11       expect(plane.status).to eq 'landed'
12     end
13     planes.each_with_index do |plane|
14       airport.request_plane_to_takeoff plane
15     end
16     expect(airport.airborne_planes.length).to eq 6
17     expect(airport.landed_planes.length).to eq 0
18   end
19 end

(Watch me unjumble my unreadable and poorly designed Rspec/Ruby test code)

One of the issues here, besides the functionality of what it's testing, is that the test code is simply unreadable. All the tests that I intended at the time are there, but they need to be split up in to component parts. This will also help isolaate the tests that aren't passing. I think I've tried to do too much in too littile space and I need to get more comfortable with Rspec syntax.

Below is where my tests stood after some rethinking.

 1 require 'capybara/rspec'
 2 require 'airport'
 3 feature 'Grand Finale' do
 4   airport = Airport.new
 5   planes = []
 6   6.times { planes << Plane.new }
 7   plane_7 = Plane.new
 8 
 9   scenario '6 planes can be created' do
10     expect(planes.length).to eq 6
11   end
12 
13   scenario '6 planes can land' do
14     planes.each { |plane| airport.landing_permission plane }
15     expect(airport.landed_planes.length).to eq 6
16   end
17 
18   scenario 'plane is denied landing permission when airport is full' do
19     planes.each { |plane| airport.landing_permission plane }
20     expect(airport.landing_permission plane_7).to eq 'permission denied'
21   end
22 
23   scenario '6 landed planes have status: landed' do
24     airport.landed_planes.each do |plane|
25       expect(plane.status).to eq 'landed'
26     end
27   end
28 
29   scenario 'after all 6 planes takeoff there are no more landed planes' do
30     planes.each do |plane|
31       airport.order_plane_to_takeoff plane
32     end
33     expect(airport.landed_planes.length).to eq 0
34   end
35 
36   scenario 'after all 6 planes takeoff their status is \'flying\'' do
37     planes.each do |plane|
38       airport.order_plane_to_takeoff plane
39       expect(plane.status).to eq 'flying'
40     end
41   end

It's much better but there is a lot more to do. Updates to follow.

Written on March 21, 2015