I have a class library that interacts with a WCF service, and uses an app.config file for its configuration. I’d like to unit test this class, but when I run my unit test, I get:
Could not find default endpoint element that references contract ‘FooService’ in the service model client configuaration section. This might be because no configuration file was found for your application or because no end point element matching this contract could be found in the client element
According to this answer, I need the app.config file to reside within my unit test project, and indeed this solves the problem. But I really don’t want to have to copy my app.config file around to unit tests every time I change it.
I suppose I could add a link to the app.config, except that I’m using SlowCheetah to handle app.config transformations, so my app.config is generated at compile time.
Is there anything I can do to get this to work, or do I just have to give up on my app.config and handle the configuration in code?
You can mock the WCF service in your tests. You can inject a version of the the service that doesn’t use a WCF service when you are unit testing the class. This way, when the tests run, they don’t need to worry about any configuration files. This is also going to lead to a better tests. Presumably you want to test your code, not the WCF service itself. It’s better to mock it’s behaviour in your tests so you can test only your code.