tags:

views:

56

answers:

5

Ive started working on a product that uses license files. These files need to be read (and verified) in order for the application to work. This causes some problems in the unit tests, without the proper license it throws exceptions.

We are using NUnit and what I need to do is either:

  • Copy the license file into the shadow copied directory before the test is run.
  • Set the working directory to the original build output folder so that file names are still valid in the temporary test folder.

I know that file access should generally be avoided in unit tests but before the refactoring can begin, we need the unit tests in place so I need this to work.

A: 

Not necessary an answer to your question, but usually when we buy a license for a software product it usually comes with some type of support. It might not be a bad Idea to just call and see if the vendor has a solution they would recommend.

jconlin
It sounds like he is the owner of the product, so I'm not sure that'll work.
Dominic Rodger
Dominic is correct, I work for the company creating the application.
mizipzor
Why the downvote? There was nothing in the question to indicate he was the owner at the time I responded?
jconlin
I have downvoted, I usually dont justify my votes but this time the reason is obvious. Its the standard downvote for "answer is not answering the question". I hope you werent offended? That was certainly not my intention.
mizipzor
+1  A: 

I think you are aiming your test too far up the chain, you could write tests for the functions further down. One blanket test is not really a good way to do it, a start though I suppose. Try to balance the amount of time spent trying to get this test to work with how that time might be spent testing smaller more targetted units of work.

Pete Duncanson
Actually, the license is read at a very low level. License files are created in a way so that you can "cherry pick" and pay only for the wanted features. The parts of the codebase not needing the license we already have tests for, but thats a tiny fraction.
mizipzor
+1  A: 

You need to write a mock class to replace the class that is reading the license file. You can use MOQ to achieve this.

Peter Marshall
That would be the first improvement once we have a better testsuite. I want to be sure the the refactoring doesnt break anything. I assume that you are stating that what I want to do simply cant be done?
mizipzor
I'm not sure if it can be done or not. I think whats we are hinting at is you are creating more of a smoke test than a unit test as its such a 10,000ft view of a test, the only feedback you'll get is did it break or not? You won't get the where did it break info.
Pete Duncanson
+1  A: 

I suggest you start with reading this book:

Working Effectively with Legacy Code

It will give you lots of insight into how to break open this type of problem. There will be some level of change to make such code testable that will have to happen without tests, but keep it as little as possible and do it very very carefully.

In your case, since injecting classes that fake reading licenses is too much of a jump, what you can do is change the class that validates the license file so that the actual validation logic is in a single method is launched from a single method that tells the rest of the class that the license is fine and make that method virtual, and then test with a subclass that overrides the method to pretend that it validated the file.

Then, once you have some tests around this class you can dump the method and subclass in favor of a properly injected class.

(Edited to respond to the fact that the validation is complex).

Yishai
The validation logic is big and complex. Even doing what you suggest is a big jump. I just want to copy a single file. It was a big mistake thinking that it would be simple.
mizipzor
@mizipzor, but can you kick of the call to that logic via a single method? If yes, it doesn't matter where the rest of the code goes after that. That single method can be overridden to avoid the behavior under test.
Yishai
+1  A: 

Disabling the Shadow Copy feature of NUnit will make the tests run in the same output folder as they were built in. But this will also block new builds (since the file is in use). A bad solution, imo, but a solution nontheless.

mizipzor