How to check rake task with RSpec?

I have a Ruby 2.1 / Rails 3.2 application that uses an asset pipeline. We also use a fragile (alpha) gem, which sometimes leads to the failure of rake assets: precompilation. I would like to write an rspec test that ensures that this rake command always passes before we pass our code.

I wrote a test in spec / asset_precompile_spec.rb that looks like this:

require 'spec_helper' require 'rake' describe 'assets:precompile' do before { MyApp::Application.load_tasks } it { expect { Rake::Task['assets:precompile'].invoke }.not_to raise_exception } end 

Then I ran it on the command line using

 rspec spec/lib/assets_precompile_spec.rb 

The result I got is as follows:

  1) assets:precompile Failure/Error: it { expect { Rake::Task['assets:precompile'].invoke }.not_to raise_exception } expected no Exception, got #<RuntimeError: Command failed with status (1): [/home/railsdev/.rvm/rubies/ruby-2.1.2/bin/...]> with backtrace: # ./spec/lib/assets_precompile_spec.rb:7:in `block (3 levels) in <top (required)>' # ./spec/lib/assets_precompile_spec.rb:7:in `block (2 levels) in <top (required)>' # ./spec/lib/assets_precompile_spec.rb:7:in `block (2 levels) in <top (required)>' Finished in 0.71247 seconds 1 example, 1 failure Failed examples: rspec ./spec/lib/assets_precompile_spec.rb:7 # assets:precompile 

I looked far and wide, and I cannot find an example to run "rake assets: precompile", which actually works in my RSpec environment. I tried explicitly loading the spec_helper.rb file, I tried explicitly requiring "factory_girl", but I can not find anything that works.

Is there a way to make a test run of this rake task running in an RSpec test?

+6
source share
1 answer

Try Rake::Task['assets:precompile:all'].invoke

instead of Rake::Task['assets:precompile'].invoke

In my case, it helped.

+2
source

Source: https://habr.com/ru/post/972270/


All Articles