[rspec-users] modules and stub

Sarah Allen
Mon, 25 May 2009 05:51:02 -0000

David Chelimsky wrote:
>
> Foo::Bar.stub(:do_something)

hmm. that''s pretty close to where I started.

> If you stub, for example, do_something on this module, but it turns
> out that do_something gets added to the module through some dynamic
> means *after the stub declaration*, the stub declaration will be
> overwritten and you''ll get surprising results. This is not just
true
> of globals - it''s true of any methods that appear through the
magic of
> metaprogramming.

Ah, the joys of dynamic languages.  After further looking at the code, I
see that the module ''require'' is intentionally inside an if,
so that it
will only get loaded as needed.  If I explicitly require file containing
the module in the spec.rb file, then it works.  I suppose the require in
the code I''m testing will only have an effect when it''s not
present in
the calling code.  Still puzzled why I could call the module before
without getting a syntax error.

Thanks so much for your help.