Jason Swett
Jason Swett

Reputation: 45104

Stubbing a class within a namespace

I have the following controller spec that works just fine right now:

# This top part is a hack
module MyModule
  class MyOAuthClient < OAuthClient
    def token_is_valid?(options)
      true
    end
  end 
end

# Here's the actual spec
describe MyModule::OAuthController do
  describe "GET callback" do
    it "works fine when token is valid" do
      post :callback, use_route: :my_module
      expect(response.code).to eq("200")
    end 
  end 
end

What I want to do is replace the monkey patch in my spec with a stub. How do I do this?

The rspec-mocks docs show examples for stubbing classes that aren't under namespaces, but it doesn't seem that you can just apply those examples to namespaced classes and have it work.

I've tried certain things already but I don't want to bias people's answers with my incorrect guesses.

Upvotes: 0

Views: 709

Answers (1)

Jason Swett
Jason Swett

Reputation: 45104

Turns out I was after any_instance:

MyModule::OAuthClient.any_instance.stub(:token_is_valid?) { true }

Upvotes: 1

Related Questions