Rails ActiveSupport: How to assert that an error is raised?

So unit testing isn’t really in activesupport. Ruby comes with a typical xunit framework in the standard libs (Test::Unit in ruby 1.8.x, MiniTest in ruby 1.9), and the stuff in activesupport just adds some stuff to it. If you are using Test::Unit/MiniTest assert_raise(Exception) { whatever.merge } if you are using rspec (unfortunately poorly documented, but … Read more

What’s the difference between raise, try, and assert?

The statement assert can be used for checking conditions at runtime, but is removed if optimizations are requested from Python. The extended form is: assert condition, message and is equivalent to: if __debug__: if not condition: raise AssertionError(message) where __debug__ is True is Python was not started with the option -O. So the statement assert … Read more

How to use “raise” keyword in Python [duplicate]

It has two purposes. jackcogdill has given the first one: It’s used for raising your own errors. if something: raise Exception(‘My error!’) The second is to reraise the current exception in an exception handler, so that it can be handled further up the call stack. try: generate_exception() except SomeException as e: if not can_handle(e): raise … Read more

Best practice for using assert?

Asserts should be used to test conditions that should never happen. The purpose is to crash early in the case of a corrupt program state. Exceptions should be used for errors that can conceivably happen, and you should almost always create your own Exception classes. For example, if you’re writing a function to read from … Read more