[rspec-devel] [ rspec-Bugs-7805 ] Stubs aren't being removed from modules

noreply at rubyforge.org noreply at rubyforge.org
Sun Jan 14 00:06:17 EST 2007


Bugs item #7805, was opened at 2007-01-14 01:59
You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3149&aid=7805&group_id=797

Category: mock module
Group: None
Status: Open
Resolution: None
Priority: 3
Submitted By: Michal Kwiatkowski (infrared)
Assigned to: Nobody (None)
Summary: Stubs aren't being removed from modules

Initial Comment:
Stubs aren't removed after running specs, which doesn't break things for modules begin tested, but can be source of bugs if you're stubbing classes from stdlib. Example code stubs File.open and manages to break RSpec code itself. Place attached Rakefile and spec in the same directory, then run "rake --trace" to see:

undefined method `<' for nil:NilClass
/usr/lib/ruby/gems/1.8/gems/rspec-0.7.5/lib/spec/rake/verify_rcov.rb:42:in `define'

Source of this error is 'total_coverage' variable which doesn't get initialized because File.open doesn't yield nor return anything useful (because it's still stubbed).

As you see in the stack report, I'm using rspec 0.7.5.

----------------------------------------------------------------------

Comment By: David Chelimsky (dchelimsky)
Date: 2007-01-14 05:04

Message:
There is a problem here but it is different from what you suggest.

I created a file called stubs_should_restore_methods.rb (attached). Here's the code and the command line session:

require 'spec'

context "stubs" do
  specify "1 - stub the open method" do
    File.stub!(:open).and_return("something")
    File.open.should == "something"
  end
  specify "2 - use File.open to create example.txt" do
    filename = "example-#{Time.now.to_i}.txt"
    file = File.open(filename,'w')
    file << "some text"
    file.close
  end
end

[david at mac tmp]$ ls -al
total 0
drwxr-xr-x   2 david  david   68 Jan 13 23:01 .
drwxr-xr-x   9 david  david  306 Jan 13 23:01 ..
[david at mac tmp]$ mv ../stubs_should_restore_methods.rb .
[david at mac tmp]$ ls -al
total 8
drwxr-xr-x   3 david  david  102 Jan 13 23:02 .
drwxr-xr-x   8 david  david  272 Jan 13 23:02 ..
-rw-r--r--   1 david  david  348 Jan 13 22:54 stubs_should_restore_methods.rb
[david at mac tmp]$ ruby stubs_should_restore_methods.rb -fs

stubs
- 1 - stub the open method
- 2 - use File.open to create example.txt

Finished in 0.001414 seconds

2 specifications, 0 failures
[david at mac tmp]$ ls -al
total 16
drwxr-xr-x   4 david  david  136 Jan 13 23:02 .
drwxr-xr-x   8 david  david  272 Jan 13 23:02 ..
-rw-r--r--   1 david  david    9 Jan 13 23:02 example-1168750953.txt
-rw-r--r--   1 david  david  348 Jan 13 22:54 stubs_should_restore_methods.rb
[david at mac tmp]$ ruby stubs_should_restore_methods.rb -fs

stubs
- 1 - stub the open method
- 2 - use File.open to create example.txt

Finished in 0.001393 seconds

2 specifications, 0 failures
[david at mac tmp]$ ls -al
total 24
drwxr-xr-x   5 david  david  170 Jan 13 23:02 .
drwxr-xr-x   8 david  david  272 Jan 13 23:02 ..
-rw-r--r--   1 david  david    9 Jan 13 23:02 example-1168750953.txt
-rw-r--r--   1 david  david    9 Jan 13 23:02 example-1168750963.txt
-rw-r--r--   1 david  david  348 Jan 13 22:54 stubs_should_restore_methods.rb

As you can see, each time the spec is run, the second spec creates a new file using File.open, even though File.open had been stubbed in the first spec.

I think the problem is that rcov is trying to use File.open to create and/or write to a file DURING the spec. If that's correct, I don't see what we would be able to do about that from RSpec.

What do you think?

Cheers,
David

----------------------------------------------------------------------

You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3149&aid=7805&group_id=797


More information about the rspec-devel mailing list