Problem stubbing class methods
Created by: dx7
I've had a problem stubbing class methods with Rspec 3. I created a spec file with code below as simple as possible to show the problem.
module M
module ClassMethods
def method_a
raise "should not execute method_a"
end
end
def self.included(base)
base.extend M::ClassMethods
end
end
class A
include M
end
class B < A
def self.method_b
"executed method_b"
end
end
describe "Example" do
it "works as expected" do
allow(B).to receive(:method_b).and_return("stubbed method_b")
expect(B.method_b).to eql("stubbed method_b")
end
it "should work but it doesn't" do
allow(A).to receive(:method_a) # why this line makes next line useless?
allow(B).to receive(:method_b).and_return("stubbed method_b")
expect(B.method_b).to eql("stubbed method_b")
end
end
Executing code above with Rspec 2.99 works fine. However I've had the following error with Rspec 3.0.3 (the spec file contains above code only):
$ rspec whatever_spec.rb
.F
Failures:
1) Example should work but it doesn't
Failure/Error: expect(B.method_b).to eql("stubbed method_b")
expected: "stubbed method_b"
got: "executed method_b"
(compared using eql?)
# ./whatever_spec.rb:34:in `block (2 levels) in <top (required)>'
Finished in 0.00729 seconds (files took 0.12529 seconds to load)
2 examples, 1 failure
Failed examples:
rspec ./whatever_spec.rb:30 # Example should work but it doesn't
Rspec installed:
$ gem list | grep rspec
rspec (3.0.0)
rspec-core (3.0.3)
rspec-expectations (3.0.3)
rspec-mocks (3.0.3)
rspec-support (3.0.3)
Any idea?