Hi I'm working on a c# MVC code first project. I've started the project and have created the Models, database frontend etc.

Now my attention has come to writing something that can read an excel document and import the data into my database.

This is a bit of a fiddly task, to say the least - so I did start making a unit test in my test project. This worked fine reading the excel and getting the values but because it didn't have access to the Context(database) wasn't enough to finish the job.

So I made a new controller/action in my MVC project and copied the start of the code in. I then wrote the bits with writing the information to the database. This then caused issues. So I changed the action into a test method and the controller into a test class. Then I could output whatever I wanted using the Trace commands and find out what was going on.

Is this the right thing to do? Maybe if it works remove the testClass/testMethod once I'm happy and then return an action? I'm thinking about doing this perhaps if I have other files I need to process in this fashion.

How do other people tackle this problem?


Thanks, when you say an extra class doing the excel stuff. I'm using EPPlus to read the excel and that's doing just fine for me.

Just talking to my college and basically I shouldn't be using the test methods as a form of debug and instead using the VS debugger. Which after a bit of tutorial seems to be the way.

  • Just talking to my college and basically I shouldn't be using the test methods as a form of debug and instead using the VS debugger. - Well I disagree at least a bit. If you're developing in a testdriven way you need to implement the testcases first. One positive effect is, that you can debug them. So of course using test methods while development is a legit approach. Although, it's a bit weird, that you're using the VS debugger instead.
    – DHN
    Oct 5, 2016 at 17:59
  • Mmm I see where you are coming from. TBO I don't know what way I'm developing at the moment. I'm relatively new to c# (although I've done a lot of PHP). I guess I'm just finding my way. My college did say to have a look at test driven development - so I guess I'll have a look, read a bit more about that. But this whole bit of the code is an import script - run once and throw away - so not sure I need all the test methods to make sure it passes the tests. Not even sure how that would work in a test driven environment. Thanks for your input though it's all good (personal) development! Oct 6, 2016 at 8:28
  • @RichardHousham Weighing up whether or not to use TDD is smart. But make sure you fully understand how that approach would actually work before ruling it out. Specifically, does EPPlus present an API that you can easily mock? Oct 6, 2016 at 10:57

1 Answer 1


The best approach is to create an extra class, which is only doing the Excel stuff. Another class should do the storing to database. It's simply separation of concerns.

That way you can test it easily in unit tests as well as utilize it. No copying of code and such ugly things. (Actually, if you need to do it that way, something is designed in the wrong way.)

So first think about, what the class is needing as input to do its job probably and pass it, via constructor or method parameter. Next thing, what should the output of the class' methods? In the end, the most important thing, the class must work regardless, who is calling it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.