On Mon, Nov 1, 2010 at 10:32 AM, David Chelimsky <[email protected]> wrote:
> Can you post a real example of this complete with spec,
> implementation, and failure message?
>
> Thx
This is the closest I could get with my limited knowledge of RSpec's
and RSpec-rails's internals. I tried to replicate a bit of
rails-settings' "fancy footwork" with method_missing, and I use a
before(:all) block here to simulate what happens in my app, where a
stub happens within a normal before block in one point and causes a
similar failure 700 lines later.
require 'spec_helper'
describe "stubbing a class method" do
class Settings < ActiveRecord::Base
@@defaults = {
"foo" => "a",
"bar" => "b"
}
def self.method_missing(sym, *args)
super
rescue NoMethodError
@@defaults[sym.to_s]
end
end
before(:all) do
Settings.stub!(:foo).and_return("c")
end
specify "works when the stub is invoked for the first time" do
Settings.foo.should == "c"
Settings.bar.should == "b"
end
specify "should not raise an error after the first time" do
lambda { Settings.foo.should == "a"}.should_not raise_exception
end
end
Running this along with RSpec-rails' other specs gives me the following failure:
1)
'stubbing a class method should not raise an error after the first time' FAILED
expected no Exception, got #<NoMethodError: undefined method `foo' for
ActiveRecord::Base:Class>
./spec/spec/rails/mocks/bug_report_xxx_spec.rb:30:
I'm not sure if the correct behavior would be to call the actual
implementation of Settings.foo, or to give a different error message.
Currently, it tries to call ActiveRecord::Base.foo .
Not using a before(:all) block prevents the failure, but as I said
it's only here as a "shorthand" for the complex specs I have on my
app. Actually declaring a .foo class method on Settings also makes it
pass, so method_missing seems to be part of the problem.
On my app's specs, I found out that calling "unstub!" explicitly on an
after block makes some specs pass as well.
--
Bira
http://compexplicita.wordpress.com
http://compexplicita.tumblr.com
_______________________________________________
rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users