I have something like this:
public byte[] EncodeMyObject(MyObject obj)
I've been unit testing like this:
byte[] expectedResults = new byte[3]{ 0x01, 0x02, 0xFF };
Assert.IsEqual(expectedResults, EncodeMyObject(myObject));
EDIT: The two ways I've seen proposed are:
1) Using hardcoded expected values, like the above example.
2) Using a decoder to decode the encoded byte array and comparing the input/output objects.
The problem I see with method 1 is that it is very brittle and requires a lot of hard coded values.
The problem with method 2 is that testing the encoder depends on the decoder working correctly. If the encoder/decoder are broken equally (in the same place), then the tests could produce false positives.
These may very well be the only ways to test this type of method. If that's the case then fine. I'm asking the question to see if there are any better strategies for this type of testing. I can not reveal the internals of the particular encoder I am working on. I am asking in general how you would solve this type of problem, and I don't feel the internals are important. Assume that a given input object will always produce the same output byte array.
myObject
go frommyObject
to{ 0x01, 0x02, 0xFF }
? Can that algorithm be broken down and tested? The reason I ask is presently, it looks like you have a test that proves that one magic thing produces another magic thing. Your only confidence is that the one input produces the one output. If you can break down the algorithm, you can gain further confidence in the algorithm, and be less reliant on magical inputs and outputs.