Try our conversational search powered by Generative AI!

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

Vote:
 

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?

#206166
Aug 06, 2019 7:04
Vote:
 

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

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

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

#206175
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?
Vote:
 
#206200
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.