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

Consider returning and depending on interfaces in ISearchClient #840

Open
AnuBargreen opened this issue Jul 31, 2023 · 4 comments
Open

Consider returning and depending on interfaces in ISearchClient #840

AnuBargreen opened this issue Jul 31, 2023 · 4 comments

Comments

@AnuBargreen
Copy link

  • Algolia Client Version: 6.14.0
  • Language Version: C# .net6

Description

ISearchClient should depend on interfaces and not concrete types. This breaks Liskov substitution principle when testing this. The concrete type SearchIndex has a internal constructor making it harder to wrap this for testing.

/// <summary>
/// Initialize an index for the given client
/// </summary>
/// <param name="indexName"></param>
/// <returns></returns>
SearchIndex InitIndex(string indexName);

Steps To Reproduce

Use Moq to create a Mock of SearchClient and setup responses to various calls:

using Moq;
using Moq.AutoMock;

AutoMocker _mocker = new();
var searchClient = _mocker.GetMock<ISearchClient>();
var searchIndex = _mocker.GetMock<SearchIndex>(); // Can't use `ISearchIndex` here because `InitIndex` returns Concrete type with internal constructor
searchClient.Setup(b => b.InitIndex("test_items")).Returns((SearchIndex)searchIndex.Object);
@rmendoza-prescriber
Copy link

Would be nice if this change can be made in the next release, I'm having issues trying to write unit test using this client

@PritamSanganiTW
Copy link

PritamSanganiTW commented Sep 19, 2023

+1 to this. It is very difficult to unit test interactions with the ISearchClient. Also related to #745

@morganleroi
Copy link
Contributor

Hello,

We will release in few days a new major version of all Algolia C# Clients. In this version, all clients returns interfaces to enhance testability.

I'll reply in this thread as soon as the client is released.

@morganleroi
Copy link
Contributor

@rmendoza-prescriber @PritamSanganiTW @AnuBargreen

The Alpha version is now released on Nuget.

If you have time, we need customer feedback about this new version. It's still not GA so you may not want to use it in production.

There is no more deps on Newtonsoft, all Algolia APIs are now available (Insights, Recommend, Query Perso, ...) and all models and routes are synced.

To help you migrate, there is a Migration guide.

Happy to help in your migration process !

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

4 participants