Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to mock BasicApi (and other) classes #209

Open
bostanigor opened this issue Jan 30, 2023 · 1 comment
Open

Unable to mock BasicApi (and other) classes #209

bostanigor opened this issue Jan 30, 2023 · 1 comment

Comments

@bostanigor
Copy link

bostanigor commented Jan 30, 2023

For some my RSpec tests I've been mocking methods that call the API like so:

before do
  expect_any_instance_of(Hubspot::Crm::Contacts::BasicApi).to receive(:get_by_id).with(contact_id: hubspot_id)
    .and_return(OpenStruct.new({ id: hubspot_id }))
end

But I'm unable to do this as the current version is not loading the Hubspot::Crm::Contacts::BasicApi constant automatically. Well, I've added my own requires into the spec/spec_helper.rb file as follows:

require 'hubspot/helpers/get_all_helper' # I don't even know why but without this require the spec won't work
require 'hubspot/codegen/crm/contacts/api/basic_api'

Still no luck as this code would load a Hubspot::Discovery::Crm::Contacts::BasicApi constant and fill it with methods in real-time but by that point the Hubspot::Crm::Contacts::BasicApi (from the codegen folder) would be already altered by my mock which leads to an error in the BaseApiClient#define_api_methods.

@client = Hubspot::Client.new(access_token: access_token)
base_api = @client.crm.contacts.basic_api
base_api.get_by_id(contact_id: user.hubspot_id)
# cannot load such file -- hubspot/codegen/crm/contacts/models/args

What if we would require the discovery module and mock the Hubspot::Discovery::Crm::Contacts::BasicApi?

require 'hubspot/discovery/crm/contacts/api/basic_api'
expect_any_instance_of(Hubspot::Discovery::Crm::Contacts::BasicApi).to receive(:get_by_id).with(contact_id: hubspot_id)
    .and_return(OpenStruct.new({ id: hubspot_id }))
# Hubspot::Discovery::Crm::Contacts::BasicApi does not implement #get_by_id 

Well because of this requiring the module loaded it doesn't have any methods! As they are all generated real-time when a BasicApi instance is created. There is no method to stub unless there is an instance present.

I don't even know how to do this anymore

@bostanigor
Copy link
Author

bostanigor commented Jan 30, 2023

I've got an idea to stub Hubspot::Discovery::Crm::Contacts::BasicApi.new to an exact instance and maybe mock it's methods instead.

require 'hubspot/helpers/get_all_helper'
require 'hubspot/discovery/crm/contacts/api/basic_api'
Hubspot::Discovery::Crm::Contacts::BasicApi.new({access_token: '123123123123'}).get_by_id
# cannot load such file -- hubspot/codegen/crm/contacts/models/contact_id (LoadError)

As the BaseApiClient#define_api_methods check for the method in a codegen class it's reasonable it would check the Hubspot::Crm::Contacts::BasicApi#get_by_id method. And it's definition (def get_by_id(contact_id, opts = {})) shows us that it will search for a contact_id.rb in a models folder. And of course there is no such file. I don't even know how that is supposed to work.
I'll probably downgrade for now as it's a mess to test. I'm up for the dynamic reflective code (that's fun to program) but there it has too much drawbacks.
I would like to have an option to somehow preload all modules and classes in the test env and be able to stub class methods that would be there already. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant