Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best practice to use asserts in Ruby, but not as part of unit tests?

I want to use an assert to raise an error within a rake task.

the_index = items.index(some_item)
assert_not_nil the_index, "Lookup failed for the following item: " + some_item

I get undefined method assert_not_nil. Can I include the assertions file in my rake task? How?

Is this a best practice, or is there a better way to do it?

Working in Ruby 1.9.2.

like image 400
B Seven Avatar asked Jan 19 '23 01:01

B Seven


1 Answers

You can actually use assertions wherever you want.

require "minitest/unit"
include MiniTest::Assertions # all assertions are in this module
refute_nil @ivar, "An instance variable should not be nil here!"

But why would you do that? Raise meaningful exceptions yourself instead.

like image 123
Simon Perepelitsa Avatar answered Feb 09 '23 13:02

Simon Perepelitsa