Virtual Happy Hour this month, Jun 28, we'll be getting a sneak preview at our soon to launch SaaS CMS!

Try our conversational search powered by Generative AI!

'EPiServer.Find.Framework.SearchClient' threw an exception in MS unit test


I have a Web API controller which use Epi Find to get data and it works perfectly in the website, but fails the MS unit test with following exception: "The type initializer for 'EPiServer.Find.Framework.SearchClient' threw an exception". The exception is thrown at this statement: var client = SearchClient.Instance;

What is the reason that the SearchClient.Instance is not working when the unit TestMethod call the Controller Get method? and how to fix it?

Aug 06, 2019 7:04

My guess is that SearchClient.Instance needs a web context which you dont have when running a unit test?

Aug 06, 2019 10:09
Helani Wanniarachchi - Aug 07, 2019 8:54
Yes I also suspect so, is there a way to fix it?

If mocking is an option for your test you could use IClient in your Web API contoller, as SearchClient.Instance is an implementation of IClient

Aug 06, 2019 10:29
Helani Wanniarachchi - Aug 07, 2019 8:53
Can you please provide me a code sample of using IClient instead of SearchClient.Instance in the Web API controller?
Aug 07, 2019 9:08
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.