5 posts about #ruby
irb may be outdated
The irb gem can be more advanced than the version that comes with the current ruby version on your system. You can add irb to your gemfile to use a newer version.
bundle add irb
Add months to a date with >>
In ruby you can add days to a date with the + method, and you can add months with >>:
require "date"
Date.today + 1
=> #<Date: 2023-07-11>
Date.today >> 1
=> #<Date: 2023-08-10>
How to spy on method calls in minitest
You can spy on a method in minitest with a Mock by calling #verify
mock = MiniTest::Mock.new
mock.expect :username
User.stub :find, mock do
user = User.find(1)
user.username
end
mock.verify
How to stub a method in minitest
In minitest you can stub a method my calling .stub and passing a method name:
class User
def self.all
42
end
end
User.stub(:all, [1, 2, 3]) do
assert_equal [1, 2, 3], User.all
end