ruby - How to test rake task callbacks -




i create test code rspec. want test callback executed or not.
task main: [:callback] means run callback before main, doesn't it?

but test failed. looks callback not executed. why?

require 'rails_helper' require 'rake'  rspec.describe 'rake::task'   before(:all)     @rake = rake::application.new     rake.application = @rake     rake.application.rake_require('rspec_before', ["#{rails.root}/lib/tasks"])   end    subject { @rake['rspec_before:main'].execute }    "expects run callback before main task"     expect{ subject }.to output(/hello, world/).to_stdout   end end 

my rake task below.

namespace :rspec_before   task :callback     @greeting = "hello, world"   end    # case 1   # in case, `callback` not executed in rspec   # in console, `callback` executed !!!!   desc "main task"   task main: [:callback]     puts @greeting   end    # case 2   # in case, `callback` executed in rspec   # task :main   #   rake::task['rspec_before:callback'].execute   #   puts @greeting   # end end 

so, i'm not sure call :callback callback, it's more of dependency. callback implies happens after main task done, whereas when do

task main: [:callback] 

what you're saying depend on other task having run first. end changing name of that, though that's sample/throwaway name question. but, digress , i'll continue calling task :callback written answer.


the main issue here when call execute on rake task, task gets executed. because there may situations don't want or need call entire dependency chain. let's add task file:

desc "secondary task" task :secondary   @greeting = 'goodbye, cruel world'    rake::task['rspec_before:main'].execute end 

if run this, want main task output goodbye, cruel world, instead of hello, world , if call dependencies, main end calling :callback override our @greeting , end outputting hello, world.

there is, however, task call entire dependency chain: invoke:

desc "secondary task" task :secondary   @greeting = 'goodbye, cruel world'   rake::task['rspec_before:main'].invoke end 

if run task, we'll see hello, world instead of goodbye, cruel world. so, having said of this, mean rspec test? need change subject to:

subject { @rake['rspec_before:main'].invoke } 

because wanting run dependencies.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -