Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Requiring a thor task in my rspec results in undefined method

I'm trying to write a very basic rspec to for my Thor task, however when trying to require (or load) the task it fails, giving NoMethodError ('undefined method ...') for the various Thor class-level methods (desc, method_option, class_option etc)

require "spec_helper"
require Rails.root.join('lib/tasks/test_task.thor')

describe 'TestTask' do

  it "is instantiated ok" do
    TestTask.new
  end
end

As you can see I'm testing in the environment of a rails app.

The thor task itself executes fine from the command line.

I've looked through the Thor specs, as suggested elsewhere (Where can I find good examples of testing a Thor script with RSpec?)

Any ideas?

like image 413
Tim Diggins Avatar asked Sep 05 '25 03:09

Tim Diggins


1 Answers

The answer that I've found is to use load rather than require (I thought I had tested this, but perhaps I was mistaken)

so:

require 'thor' load File.join(Rails.root.join('lib/tasks/test_task.thor'))

like image 142
Tim Diggins Avatar answered Sep 07 '25 20:09

Tim Diggins