Pergunta

I am new to unit testing, so I am probably misunderstanding something big, but I have been asked to create some unit tests for my WCF service. It's a very simple service that executes a stored procedure and returns the result. The second line in my operation is this:

string conn = ConfigurationManager
    .ConnectionStrings["AtlasMirrorConnectionString"].ConnectionString;

Everything works fine when deploying the service, but under unit testing, it seems that the config file becomes invisible. ConfigurationManager.ConnectionStrings["AtlasMirrorConnectionString"] becomes a null reference and throws accordingly.

How do I include my config file in the tests? Right now, the only behavior I'm able to test is the handling of missing config files, which is not terribly useful.

Foi útil?

Solução

asked again and again and again and answered by me last week and this week as well :)

if you have your unit tests in another project (VS generated test project, class library etc...) just create an app config for that unit test project and put the same configuration keys as you have in the project which works.

of course I am simplifying because you could absolutely want to customize those keys with specific test values, but as a start copy what works then customize in case you want to point to another database, machine etc... :)

Outras dicas

If you want your unit test to always have the same values as your project then you can use the following line as a post-build event in the test project

copy /Y "$(SolutionDir)ProjectName\App.config" "$(TargetDir)TestProjectName.dll.config"

You'll have to decorate the test class or method with the DeploymentItemAttribute to deploy the configuration file into the test directory.

Use something like this on your TestClass (this assumes that you have a copy of the app.config local to your testclasses):

[DeploymentItem("app.config")]
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top