EngineContext.Current.Resolve on test methods.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
7 years ago
Hi everyone, i'm very new in nopCommerce and i try to develop some unit tests for my plugin.

So my method use an extension method (Nop.Services.Seo.SeoExtensions.ValidateSeName()) that uses EngineContext.Current.Resolve<SeoSettings>()

When i run my method on browser it's work well.But when i run on test it throws ArgumentNullException

Message  "Value cannot be null.\r\nParameter name: instance"
Stack Trace


at Autofac.RegistrationExtensions.RegisterInstance[T](ContainerBuilder builder, T instance)    at Nop.Core.Infrastructure.NopEngine.RegisterDependencies(NopConfig config) in C:\\CompSRC\\Team\\Main\\CommercePortal\\nopCommerce\\Libraries\\Nop.Core\\Infrastructure\\NopEngine.cs:line 57    at Nop.Core.Infrastructure.NopEngine.Initialize(NopConfig config) in C:\\CompSRC\\Team\\Main\\CommercePortal\\nopCommerce\\Libraries\\Nop.Core\\Infrastructure\\NopEngine.cs:line 89    at Nop.Core.Infrastructure.EngineContext.Initialize(Boolean forceRecreate) in C:\\CompSRC\\Team\\Main\\CommercePortal\\nopCommerce\\Libraries\\Nop.Core\\Infrastructure\\EngineContext.cs:line 26    at Nop.Core.Infrastructure.EngineContext.get_Current() in C:\\CompSRC\\Team\\Main\\CommercePortal\\nopCommerce\\Libraries\\Nop.Core\\Infrastructure\\EngineContext.cs:line 54    at Nop.Services.Seo.SeoExtensions.GetSeName(String name) in C:\\CompSRC\\Team\\Main\\CommercePortal\\nopCommerce\\Libraries\\Nop.Services\\Seo\\SeoExtensions.cs:line 251    at Nop.Services.Seo.SeoExtensions.ValidateSeName[T](T entity, String seName, String name, Boolean ensureNotEmpty) in C:\\CompSRC\\Team\\Main\\CommercePortal\\nopCommerce\\Libraries\\Nop.Services\\Seo\\SeoExtensions.cs:line 197    at Comp.Pano.Integration.Services.Integrations.CategoryIntegrationService.SyncFromDataSource(CategoryDTO category, String dataSourceName, String dataSourceIdentification) in C:\\CompSRC\\Team\\Main\\CommercePortal\\Comp.Pano.Integration\\Services\\Integrations\\CategoryIntegrationService.cs:line 180



When i create a NopEngine instance and initialize it with new NopConfig it's try to connect database for getting Stores and offcourse throw exception.

My question is How can i test a method which use EngineContext.Current.Resolve<T>() (I prefer replace T with mock instance)
7 years ago
I can't find any good solution for this issue , but i have to focus  deadline , so i create a litle hacky solution.

I create a interface for wraping SeoExtensions that named ISeoExtensions ,on test i create a mock instance of interface and Stub methods that i use. If i have to use another extension method which uses EngineContext.Current i will crate another interface and wrap it too.


I don't like it but i have to use this solution.

If you find/know another solution please notify me.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.