Can I override task :environment in test_helper.rb to test rake tasks?

后端 未结 3 1508
Happy的楠姐
Happy的楠姐 2021-02-20 14:12

I have a series of rake tasks in a Rakefile which I\'d like to test as part of my specs etc. Each task is defined in the form:

task :do_somthing => :environme         


        
相关标签:
3条回答
  • 2021-02-20 14:18

    My "solution" to a similar problem was to extract all the logic from my .rake files and create classes to perform the tasks, leaving just a one-line call in the rake file, which I felt confident in not testing too hard. The classes could then be tested pretty much normally.

    I don't know how well this would stand up to a complex set of interdependent tasks that maintain some far-reaching state: probably not well, but then again that would most likely be an indication of some other design problem...

    I'm curious to see if I've missed something better.

    EDIT: There used to be a blog post here that (a) says the same thing and (b) says it better. Looks like he said it first, too.

    0 讨论(0)
  • 2021-02-20 14:27

    when you are running tests environment is that is being loaded is test.

    so you are interacting with test database only.

    So i dont see any reason to override your rake task in test_helper.rb

    0 讨论(0)
  • 2021-02-20 14:37

    I think you are looking for this line: require(File.join(RAILS_ROOT, 'config', 'environment')) it's exactly what you find in "task :environment" implementation

    I use it to test my rake tasks using rspec

    0 讨论(0)
提交回复
热议问题