Here is the class I am testing contained in Foo.rb :
class Foo def bar return 2 end end
Here is my test contained in Foo_spec.rb :
require "./Foo.rb" describe "Foo" do before(:all) do puts "#{Foo == nil}" Foo.any_instance.stub(:bar).and_return(1) end it "should pass this" do f = Foo.new f.bar.should eq 1 end end
I get the following output:
false F Failures: 1) Foo Should pass this Failure/Error: Foo.any_instance.stub(:bar).and_return(1) NoMethodError: undefined method `any_instance_recorder_for' for nil:NilClass # ./Foo_spec.rb:6:in `block (2 levels) in <top (required)>' Finished in 0 seconds 1 example, 1 failure Failed examples: rspec ./Foo_spec.rb:9 # Foo Should pass this
I have advised a document and the above example runs on my machine (so this is not a problem with the rspec code), but it does not give me any information about what I may be doing wrong. The error message is also rather confusing, as it tells me not to call .any_instance on nil:NilClass , but, as I proved with my output, Foo not nil . How can I call .any_instance.stub on my custom object?
I am using Ruby 1.9.3 and rspec 2.14.5 .
ruby rspec
Seanny123
source share