3

I have a service which acts on a file uploaded. I have a function in the service which basically converts the uploaded(.xlxs) file into workbook. I would want to write test case for this particular function. (Not uploaded to server but just to browser) Can anyone suggest on how to access a test file(say test_something.xlxs) from inside the unit test scenario. I would want to read the file as a binary string and pass on the object to the service methods.

Unit testing framework i use is Karma + Jasmine for angular.

sriram
  • 407
  • 2
  • 8
  • 19

1 Answers1

6

It's a pain, because by the time you get into your harness, you're in sandboxed-browser-land and you can't really access external files easily. Karma works by basically creating a fake HTML page wrapper that it loads into a browser for testing, via PhantomJS, Jasmine, etc. By the time your test harness gets going, that browser is already running and it's too late to start doing odd things out at the system level.

The way you can get around this is with the 'files' key in the Karma configuration. You can add test fixtures here, and Karma will emit them into the page. When you get into binary data, what you have to do is have a build task convert it into something Javascript can tolerate. Typically you'll make a JSON file that contains an escaped string (like Base64) that you can decode back into its raw data.

The https://github.com/karma-runner/karma-ng-html2js-preprocessor HTML2JS preprocessor for Karma does something very similar, and could be a good starting point for you to fork from. It's designed to let you embed HTML files into a test harness for various purposes, and it works by doing the same thing: encoding them into an embeddable format before Karma starts. Using the same mechanism you can embed almost anything you want, if you're willing to bridge the gap with a little pre-processing code.

Chad Robinson
  • 4,575
  • 22
  • 27
  • I would have to convert the excel file into a JSON first? And then read it like its mentioned here ? http://stackoverflow.com/questions/17370427/loading-a-mock-json-file-within-karmaangularjs-test – sriram Jul 15 '14 at 16:57
  • Yes, that's the common pattern anyway. A raw XLS might be frustrating to work with though, because once you have it encoded, what then? You can decode it... but you can't call Excel with it (unless you're doing something odd like calling an ActiveX object from MSIE?). If you want to access this from raw JS have you considered a CSV or similar? – Chad Robinson Jul 15 '14 at 17:28
  • Excel parser part is fine. I already have a Excel parser which is what i want to unit test. I see that the file will be served if we include in Karma. Cant we just access it from jasmine directly ? – sriram Jul 15 '14 at 17:45
  • No... because by the time you get into Jasmine you're inside the browser. There are no JS methods for directly opening local files and working with them in this way. If you don't mind some manual input in your test cycle you can use things like the FileReader API, but all of those are going to require SOME interaction from the user to deal with browser security controls. Unless you want to disable those? – Chad Robinson Jul 17 '14 at 11:45