You basically can't mock this in a sensible fashion with rspec-mocks because all of our mocking suite is predicated on already having loaded the class, at which point your method has already been invoked. Even if you could control the loading and pre-emptively mock it during loading rspec-mocks are not designed to survive across tests and loading classes happens only once so would leak mocks amongst tests.
Why are you attempting to mock this method? One of the general peices of advise with mocking is not to mock things you don't own, so if the answer is to make assertions upon what including the gem does, you are better off making those assertions based on the result rather than mocking it. If the answer is because it makes some expensive API call you'd like to avoid in tests, I would suggest instead either using something like VCR to mock out the API call, (which would have to be done before loading 'a') or to instead subsitute this module entirely for a fake "test adapter" you control in its entirity (an implementation of the adapter pattern). Cheers Jon On Thursday, 26 January 2023 at 09:30:24 UTC zha...@payrailz.com wrote: > Hello, > I have a class that includes a module from a gem and leverages a class > method from the module: > > class A > include ThirdPartyGem::ModuleName > > method_from_module option1: 'a thing', option2: 384 > end > > My spec test looks like: > > require 'spec_helper' > require 'a' > > Rspec.describe A do > ... > end > > I want to know how i can mock "method_from_module" since it would get > executed on the "require" in the spec. There are instance methods that I > want to test but for brevity I have left those out of the examples above. > I was trying to think about using "class_double" in RSpec.configure, but I > still intend on testing that class. > > Thanks for any ideas. > > > -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/9c528fb1-3fe3-44b9-a7d5-58f2c664893dn%40googlegroups.com.