volume_up

A critical vulnerability was discovered in React Server Components (Next.js). Our systems remain protected but we advise to update packages to newest version. Learn More

AI OnAI Offsmart_toy

'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?

#206166
Aug 06, 2019 7:04

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
- 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

#206175
Aug 06, 2019 10:29
- 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?
* 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.