ruby - RSpec Raising an Exception - expected exception, but nothing was raised response? -
i doing coding challenge , trying make 1 rspec test pass, unsure why code not passing. error getting after running rspec is:
1) plane#land raises error if landed failure/error: expect { plane.land }.to raise_error 'plane can not land on ground' expected exception "plane can not land on ground" nothing raised # ./spec/plane_spec.rb:22:in `block (3 levels) in '
the rspec test code is:
describe plane let(:plane) { plane.new } describe '#land' 'raises error if landed' plane.land expect { plane.land }.to raise_error 'plane can not land on ground' end end
end
and main code is:
class plane def initialize @in_air = true end def land fail "plane can not land on ground" if already_landed? @in_air = false end end
i have tried substituting plane plane.new , tried using raise
instead of fail
, have double checked, can not see why not working.
many thanks
you haven't defined already_landed?
method. means calling already_landed?
raise nomethoderror: undefined method 'already_landed?'
exception, never expected plane can not land...
exception. , therefore expectation not pass.
just add already_landed?
method model:
class plane def initialize @in_air = true end def land fail "plane can not land on ground" if already_landed? @in_air = false end def already_landed? !@in_air end end
btw: makes sense newly created plane in_air
? expect in_air
false
on initialization , need start
first. change behavior:
class plane attr_accessor :flying def fly # no exception here, assume save continue flying, when in air self.flying = true end def land fail 'plane can not land on ground' if landed self.flying = false end def landed !flying end end plane = plane.new plane.land #=> runtimeerror: plane can not land on ground
Comments
Post a Comment