Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unit test this?

I've been playing around with .NET SpeechSynthesizer lately.

and I've got a method that takes in a string and creates a .wav file out of that string. but how do I unit test this method ?

seems to me like this situation is one in which unit testing cannot help you. am I right ?

like image 262
Attilah Avatar asked Dec 19 '25 08:12

Attilah


2 Answers

What do you want to test here?

Does the .NET SpeechSynthesizer API write the wave file or does it output something to your code which writes the wave file?

Testing this is the same as testing any other dependency. First isolate it (thin & mockable wrapper arround the NET SpeechSynthesizer API). Then validate your code gives the wrapper what you think it should be given and acts on any returned data the way it should act. Leave testing the .NET SpeechSynthesizer API to someone else (the creators of the API).

Testing the wrapper is an intergration test (don't let the name stop you from doing it in a TDD manor if thats your thing), and you will likely have to be quite loose on what you validate ("Speak(string) causes the output file to grow"). But again you don't need to validate the API works.

like image 89
Michael Lloyd Lee mlk Avatar answered Dec 20 '25 23:12

Michael Lloyd Lee mlk


When code has side effects like that, its not a clean layer test, but you can definitely test the side effects. Write the result to your temp dir. Verify the file is actually written to. Verify the format by loading it as a wave file.

To verify whats actually in the WAV file, well you probably don't want to analyze the wave file directly. In that case your test needs to monitor what parameters are sent to the WAV generator, to verify they are as expected.

like image 21
Frank Schwieterman Avatar answered Dec 20 '25 23:12

Frank Schwieterman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!