简体   繁体   中英

Writing test case in Rspec to handle syntax error

I am new to Rspec. I am writing a spec for a controller. Is it possible to catch syntax errors in the code with spec ie I would like to write case for handling syntax error in controller with Rspec. Thanks in advance.

in theory you could pass a piece of code as a string to eval or load a specific file that contains syntax errors and rescue those with rescue Exception => e .

but may i ask WHY?

Use eval like bellow.

describe "Backreferences to Non-Existent Capturing Groups" do
  describe '/(a)b\1/' do
    it { expect { eval '"aba".match(/(a)\1/)' }.to_not raise_error }
  end

  describe '/(a)b\2/' do
    it { expect { eval '"aba".match(/(a)\2/)' }.to raise_error(SyntaxError) }
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM