Re: [rspec] Failing to stub "test" in an object , because "test" is already defined in Kernel

22 views
Skip to first unread message

David Chelimsky

unread,
Apr 9, 2013, 4:31:34 PM4/9/13
to rs...@googlegroups.com
Hey Constantin,

Hope you are well.

I'd recommend not overriding Ruby's "test" method, or any core method. Got a different name that works as well in context?

Cheers,
David


On Tue, Apr 9, 2013 at 3:24 PM, Constantin Gavrilescu <comisaru...@gmail.com> wrote:
I'm trying to stub the method "test" on an object, and I cannot do it with rspec.

Example with a simpler case:

    o = Object.new
    o.stub!(:test).and_return "lol"
    o.test.should == "lol"

Error:
    Failure/Error: o.test.should == "lol"
    NoMethodError: private method `test' called for #<Object:0x13cf47e4>   

This is probably because Kernel implements #test and stubs are done with method_missing.
What's the recommended way to deal this this?

--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/rspec/-/cd5FnRaMQJsJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Constantin Gavrilescu

unread,
Apr 9, 2013, 4:35:43 PM4/9/13
to rspec...@rubyforge.org
I'm trying to stub the method "test" on an object, and I cannot do it with rspec.

Example with a simpler case:

    o = Object.new
    o.stub!(:test).and_return "lol"
    o.test.should == "lol"

Error:
    Failure/Error: o.test.should == "lol"
    NoMethodError: private method `test' called for #<Object:0x13cf47e4>   

This is probably because Kernel implements #test and stubs are done with method_missing.
What's the recommended way to deal this this?



--
Un fleac... m-au ciuruit.

Samer Masry

unread,
Apr 9, 2013, 4:54:58 PM4/9/13
to rspec-users
It's because :test was defined a private method and you're trying to access it publicly

class Object
  private
    def test
    ...
    end
end

There are different approaches to testing private methods.
  http://blog.jayfields.com/2007/11/ruby-testing-private-methods.html
-or-
 you can test the methods that call the private method.


_______________________________________________
rspec-users mailing list
rspec...@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users



--
DryBlis - www.dryblis.com
Samer Masry | Rails Programmer
11 Ambler Ln, Emeryville, CA 94608
Tel.:510-991-7523 | Cel.: 714-814-8508

Myron Marston

unread,
Apr 9, 2013, 5:53:21 PM4/9/13
to rs...@googlegroups.com
I believe it will work if you use a `double` rather than `Object.new`.  RSpec treats test doubles vs. partial mocks a bit different with respect to method visibility.  On a partial mock, rspec does not change the method visibility of existing methods...so since `test` is an existing private method on `Object`, it remains private.  A test double is treated as a "blank slate", and thus RSpec makes any method you stub on it public.

HTH,
Myron
Reply all
Reply to author
Forward
0 new messages