Prepending a class seems to make class method unstubbable
Created by: Ben-M
Prepended class methods cannot be stubbed. Calling them in tests after they have been stubbed raises an error.
require 'benchmark'
module TaskAnalytics
def import_tasks
puts Benchmark.measure { super }
end
end
class Task
class << self
prepend TaskAnalytics
end
def self.import_tasks
puts "importing tasks"
end
end
describe Task do
it "gets stubbed" do
Task.stub(:import_tasks) { 123 }
Task.import_tasks.should == 123
end
it "does not throw an error if the stubbed method is reused" do
expect { Task.import_tasks }.to_not raise_error
end
end
➜ rspec_bug rspec bug.rb
0.000000 0.000000 0.000000 ( 0.000037)
FF
Failures:
1) Task gets stubbed
Failure/Error: Task.import_tasks.should == 123
expected: 123
got: nil (using ==)
# ./bug.rb:22:in `block (2 levels) in <top (required)>'
2) Task does not throw an error if the stubbed method is reused
Failure/Error: expect { Task.import_tasks }.to_not raise_error
expected no Exception, got #<NoMethodError: super: no superclass method `import_tasks' for Task:Class> with backtrace:
# ./bug.rb:5:in `block in import_tasks'
# ./bug.rb:5:in `import_tasks'
# ./bug.rb:26:in `block (3 levels) in <top (required)>'
# ./bug.rb:26:in `block (2 levels) in <top (required)>'
# ./bug.rb:26:in `block (2 levels) in <top (required)>'
Finished in 0.00227 seconds
2 examples, 2 failures
Failed examples:
rspec ./bug.rb:20 # Task gets stubbed
rspec ./bug.rb:25 # Task does not throw an error if the stubbed method is reused
This seems connected to 384
Note: Bugs were edited out of this example thanks to feedback from @JonRowe