简体   繁体   中英

Web.config Configsource and file attributes not applied when unit testing

I have a Appsettings type initializer in my main project, which gets strings from appSettings in my web.config file. This appSettings section and my connectionStrings section use attributes like configSource, file to reference values.

It all works, except for when my unit testing project tries to test it.

An error is thrown in the type appSettings type initializer, that the appSettings/connectionStrings values in web.config aren't defined.

How can I get my Applicaton to intiailize properly so the web.config is accessible? Do I need to initialize an instance my main project ( the MVCApplication )?

It's causing Entity framework to have no connection string also, cause value not set in web.config

I suggest adding an App.config to your Unit test project and reference your /Configs/AppSettings.local.config within your app.config.

<configuration>
   <appSettings file='/Configs/AppSettings.local.config' />
</configuration>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM