1

Project A - Web Application/Website
Project B - Unit Test Application

I'm familiar how to do Unit Tests in multiple projects if it's something such as incrementCounter(ByRef nCounter As Integer) or getSquare(ByVal nValue As Integer). However, I'm being asked to create a Unit Test in Project B that calls a function in Project A and verifies a new record in a table. My issue is when I do so, it builds fine but has a runtime exception when trying to connect to the database (using a config file). My question is, do I need to have the same config file in Project B that is in Project A, or can Unit Testing a function in a website in this manner not be accomplished?

I would prefer to put the Unit Test in Project A but I don't have approval to do that at this moment (boss is on vacation and unreachable). I've also though of conditional statements to see if the function is being called by Unit Testing or through the browser, but that might be more work than is required. Any and all help is appreciated. Thanks.

UPDATE The actual error I'm getting is...

Class.Function-->The key 'OraConnection.ConnectionString' does not exist in the appSettings Configuration section.

I'm going to see if it's just a setting or if it's in the app.config file because I've copied it over to the other project, but am still getting the error. Thanks.

UPDATE 2
After looking around some more, Project A is a class library. However, I talked to a coworker and they informed me that the web.config files loads whatever settings are in the app.config file on launch. Also, none of the app.config files I have seem to have the database connection so it seems like a sourcing issue (or their clever way to keep things hidden). I'm trying to find a properly configured app.config file and then will test. Thanks everyone.

XstreamINsanity
  • 4,176
  • 10
  • 46
  • 59
  • Crossing from one project to another which is also calling a database is by definition not a unit test; but an integration test. This could be a good opportunity to take a step back and look at the process by which the testing is being approached and perhaps provide insight to your teammates and boss as it seems very ad-hoc at the moment. – Aaron McIver Dec 28 '10 at 15:14
  • :) That's exactly what another co-worker said to me. I'm simply calling it unit testing because of the files used and the project they wish me to have the file in. Otherwise I would definitely call it integration testing. Thanks. – XstreamINsanity Dec 28 '10 at 15:24
  • This is a perfect example of why to separate database code from presentation code. This database code should not be part of the web site. It should be in a separate class library. This would make it easier to test. – John Saunders Dec 28 '10 at 19:23

3 Answers3

0

It uses the config file of the test project. So you need to place the connection string in the config file of the project that contains the test.

Also see answer to this question: Can someone provide a quick App.config/Web.config tutorial?

Community
  • 1
  • 1
Shiraz Bhaiji
  • 64,065
  • 34
  • 143
  • 252
  • Well then that makes me ask this question: Since the project I'm calling the function from (the Unit Test project), would I need a web.config file or an app.config file. The function is from the website, but it's being calling in a non-website project. Thanks for the link. – XstreamINsanity Dec 28 '10 at 15:25
  • Yes you need an app.config file in your test project – Shiraz Bhaiji Dec 28 '10 at 15:29
  • Alright, I'll be giving that a try shortly. Thanks for the help. – XstreamINsanity Dec 28 '10 at 15:30
0

The big question is how does the Project B talk to Project A. Is Project A a dll that is referenced in Project B, or is Project A some kind of service which needs to be running separately. If it is the first case, you should not get be getting this error. If it is the second case, you should check that Project A is running when you run the unit test on Project B.

Kinjal Dixit
  • 7,777
  • 2
  • 59
  • 68
  • It's the second case. Project A is a website. I'm referencing it in Project B. I'm going to try the other answer and let see if that works. – XstreamINsanity Dec 28 '10 at 15:22
0

This probably doesn't answer your question very well, but I've found it much easier to abstract most of the logic to a business tier and unit test that. Even if you're using Entity Framework for your CRUD you can put your model in a business (or data or whatever) tier and unit test your functions.

Right now we are developing an aspx/silverlight app using EF 4 and have the model and Domain Services in a business project. It not only makes things easier to test, but we can share the same logic between the web and separate windows services that are running on the server.

Noel
  • 600
  • 16
  • 37