Rspec: Expecting a Rake task to be called
This seems to be obvious, but you can expect Rake tasks to be called in RSpec.
it 'deletes all Users' do
FactroyBot.create(:user)
expect(Rake::Task['notify:critical_operation']).to receive(:invoke)
expect { described_class.clean }.to change(User, :count).from(1).to(0)
end
Note: Try to avoid logic in rake tasks and prefer to just call classes in them.
Example:
desc 'Some task'
task :some_task do
SomeClass.new.run
end
Related cards:
RSpec: Ensuring a method is called on an object that will be created in the future
rspec >= 3.1 brings a method and_wrap_original
. It seems a bit complicated at first, but there are use cases where it helps to write precise tests. For example it allows to add expectations on objects that will only be created when your code is ...
Hide a Rake task from the `rake -T` list
A Rake task appears in rake -T
if it has a description:
desc 'Compile assets'
task :compile do
...
end
To not list it, simply omit the description:
task :compile do
...
end
You can also hide a Rake task that has been defi...
How to use Simplecov to find untested code in a Rails project with RSpec and Cucumber
Simplecov is a code coverage tool. This helps you to find out which parts of your application are not tested.
Integrating this in a rails project with rspec, cucumber and parallel_tests is easy.
- Add i...
How to make changes to a Ruby gem (as a Rails developer)
At makandra, we've built a few gems over the years. Some of these are quite popular: spreewald (> 1M downloads), active_type (> 1M downloads), and geordi (> 200k downloads)
Developing a Ruby gem is different from developing Rails applications, w...
Use a Bash function to alias the rake command to Spring binstubs or "bundle exec" fallback
There are different ways to run rake:
- On Rails 4.1+ projects, you have Spring and its binstubs which dramatically improve boot-up time for Rake and similar. You need to run
bin/rake
to use them. - On older projects, you want to run "bundle ex...
RSpec: How to check if a string contains terms in a desired order
There seems to be no built-in matcher in RSpec to check if a string contains terms in the desired order. A simple workaround is to use a regular expression that also matches newlines (m
-modifier).
Cons:
- The readability when terms need to be...
Linux: How to add a task bar to VNC displays
If you are using VNC to run Selenium tests, it may be hard to see what's going on since by default there is no list of open windows and Alt
+Tab
won't work.
Solving that ...
Monitor a Rake task with God
In order to monitor a Rake task using God your Rake file must write a file with its process ID (PID) to a path determined by God. This way God can check whether the Rake process is still alive.
Here is how to do this:...
Adjust cron jobs to allow full backtraces for rake tasks
As we get an exception notification, when a cron job fails, we wish to have the full backtrace in this mail. A rake task doesn't output the full backtrace by default, so you need the --backtrace
option.
Trigger
You will find fail mails wit...
Find Where a Rake Task is Defined
You can use rake --where task
to find the source location that defines task
:
bundle exec rake --where assets:precompile
rake assets:precompile /home/henning/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/precompiled_assets-0....