Unit testing, manual testing and code complexity

LeloLelo Member ✭✭

Since the start of my project I have tried to implement unit testing - however I have ended up to manually test with the unit tests instead. And then commenting them out - creating solid, a bit complex and untestable methods.

I am worried since I don't have that many tests: https://softwareengineering.stackexchange.com/questions/135047/should-i-avoid-private-methods-if-i-perform-tdd - but leaving them (without commenting them away) I would have ended up with lots of small public methods everywhere. When writing the actual code for the program I would have a bad encapsulation as a result.

Overall I would say I have very few methods but that does a lot of things. Its easier to code having less methods to worry with - but I end up with a project with little tests which worries me.

This is an example - A method that was built up by smaller methods at first - of which was unit tested.
public async Task<string> UploadFileToStorage(MediaFile mediaFile) { // Get the reference to the block blob from the container CloudBlockBlob blockBlob = null; bool findUniqueName = true; while(findUniqueName) { string fileName = RandomString(20); fileName += ".jpg"; blockBlob = container.GetBlockBlobReference(fileName); bool existed = await blockBlob.ExistsAsync(); if (!existed) { findUniqueName = false; } } // Upload the file await blockBlob.UploadFromStreamAsync(mediaFile.GetStreamWithImageRotatedForExternalStorage()); string url = blockBlob.Uri.ToString(); string appKey = await App.GetApplicationKey(); UploadMediaReference(appKey, url); return url; }

Right now I can't create a unit test (or would it rather be called integration test - since it involves both the program and storage over the internet?). It's àwait App.GetApplicationKey) which is the trouble. It uses Xam.Plugins.Settings and generates an error because it's called from a test project (MsTest) https://forums.xamarin.com/discussion/45475/xam-pcl-settings-plugin-gives-error-in-test-project.

Does it mean I for testing purposes should create an overloaded method - providing a mock appKey? - that my MsTest test method can call?
I would not like that since it create a maintenance problem - if I would need to change something I would have to do it in 2 places. However doing it would assure me that the upload works after asserting the url.

Did I made errors constructing the method this way? Having the original test methods still around with this method divided into 3 or 4 smaller public methods - would it have been better?
Well that would have led me to have 3-4 calls from actual app project instead of one.

I can't really decide - and I have a difficulty implementing unit testing my project. What are your experiences with unit testing and what would your advice be?

Sign In or Register to comment.