API
Version 7.x > API > Could not load file or assembly 'CMS.DataProviderSQL'... when unit testing View modes: 
User avatar
Certified Developer 8
Certified Developer 8
nrinat-ecentricarts - 11/21/2012 1:38:45 PM
   
Could not load file or assembly 'CMS.DataProviderSQL'... when unit testing
Hello

I am using the following code to authenticate a user in a method I call from the page load event of a TemplatePage.
UserInfo userInfo = UserInfoProvider.GetUserInfo(UserName);

It works fine when I load the Kentico page template; However, When I call the same method from a unit test project I get the following exception:

The type initializer for 'CMS.SettingsProvider.BaseInfo' threw an exception.
Could not load file or assembly 'CMS.DataProviderSQL' or one of its dependencies. The system cannot find the file specified.":"CMS.DataProviderSQL

I would appreciate any lead to what I might be doing wrong.

Thank you!

Norm

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 11/22/2012 8:27:45 AM
   
RE:Could not load file or assembly 'CMS.DataProviderSQL'... when unit testing
Hi,

Just to be sure, could you please check that the unit test project has references to the CMS.DataProviderSQL.dll file?

Best regards,
Juraj Ondrus

User avatar
Certified Developer 8
Certified Developer 8
nrinat-ecentricarts - 11/22/2012 9:07:05 AM
   
RE:Could not load file or assembly 'CMS.DataProviderSQL'... when unit testing
Thank you Juraj - excellent point! The reference was missing.
I made sure to reference all CMS DLLs and copy the same configuration to the unit test project.

However, the following problems make me wonder if I'm taking the right approach trying to unit test a part of a CMS system outside of the CMS context.

A. After referencing all DLLs and copying the config content I get the following exception:
[SystemMethods]: System events are not initialized.
You must call CMSContext.Init() before running any of your methods.
In web application, the web.config file must contain registration for the CMSApplicationModule:
<add name="CMSApplicationModule" type="CMS.CMSHelper.CMSApplicationModule, CMS.CMSHelper"/>.

B. If I try to call CMSContext.Init() in preparation to unit test my method I get the following error:
The application relative virtual path '~/' cannot be made absolute, because the path to the application is not known.

C. In conclusion I am wondering: Can I unit test code with Kentico API functions outside of the CMS web context? If the answer is yes, is there any thumb rule or instructions to follow on how I should prepare the system manually?

Thanks again!

Norm

User avatar
Certified Developer 8
Certified Developer 8
nrinat-ecentricarts - 11/23/2012 12:46:51 PM
   
RE:Could not load file or assembly 'CMS.DataProviderSQL'... when unit testing
I found my problem. I tried to create a mock http context before calling the Init function.
I disabled the http context creation and called only the Init function and it works fine.

Thanks!