algoliasearch-client-csharp
algoliasearch-client-csharp copied to clipboard
Consider returning and depending on interfaces in `ISearchClient`
- 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.
https://github.com/algolia/algoliasearch-client-csharp/blob/0c2faadf050c9c9801500d7bc603fdbc33aad696/src/Algolia.Search/Clients/ISearchClient.cs#L45-L50
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);
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
+1 to this. It is very difficult to unit test interactions with the ISearchClient
. Also related to #745
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.
@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 !