RSpec mocking all methods on object -


I am working on some RSPEC tests where one of my initial parameters is "foreign" class, which means that I There is no clue from where it came. It has been injected from a framework which I am using. In my code, I need to use this object for various things, mostly to call methods for passing this data (especially no data extraction) to this object.

Is it possible to copy something completely, so that I can verify that fixed, undefined, methods are called

  # of this object example of what can be (: my_obscure_object) {RSpec.complete_mock} #NOTE: it does not work ... it "also verify. whatever method is referred to as" expect (My_obscure_object) .To get (: whatever) .with (some_data) my_class.work this is ".sub_whatever is called a verified method" expect the end (my_obscure_object.sub_thing) Rapt .To (: sub_whatever) .with (some_data) my_class.  

To solve this method, I can currently think that creating an object and stub every possible method, such as

  ( : my_obscure_object) to #an example of a difficult method to solve my problem {} before Object.new (: each) my_obscure_object.stub (: whatever) {} void my_obscure_object.stub (: whatever2) {} void my_obscure_object.stub (: whatever3) {} void my_obscure_object.stub (: whatever4) {} void my_obscure_object.stub (: whatever5) {} void OB = Object.new ob.stub (: sub_whatever) {} void my_obscure_object. Stub (: sub_th Ings) {OB} end ... hope this is "whatever is called" is expected (my_obscure_object). (To receive whatever) .with (some_data) my_class.work verified end "it. (My_obscure_object.sub_thing). To get (: sub_whatever) .with (some_data) my_class.work2 end  

Is there a solution to my problem?

You make a test double All method will accept calls using as_null_object , as follows:

  Description "So Example" Do the #an example that this object may appear to show (My_obscure_object) {. Ltd. ( "fuzzy") as_null_object} go (: some_data) { 'foobar'} This hope is called "confirmation method" Kwhatever (My_obscure_object) .To get (: whatever) .with (some_data) also my_obscure_object.whatever (some_data) end to this "verified .sub_whatever method is called" hope (my_obscure_object) .To receive_message_chain (: whatever ,: sub_whatever) My_obscure_object.whatever.sub_whatever end end  < / Pre> 

Note that the received_message_chain method is available only as available in RSpec 3.0.0.beta2 from GitHub and you are not allowed to enter If you want to use the old version of RSpec or specify the logic passed to specify the expected logic values, you can rewrite that test using Individual Trial.


Comments