nsubstitute - Verifying function call order in a unit test -
i want unit test verifies 2 function calls happen in correct order. in example, first function encrypts file , saves file system, , second function sends encrypted file 3rd party processor (via ftp).
i using nsubstitute mock framework , fluentassertions aid in test verification. not seem can achieve nsubstitute out of box.
public void senduploadtoprocessor(stream stream, string filename) { var encryptedfilename = filenamebuilder.buildencryptedfilename(filename); fileencrypter.encrypt(stream, filename, encryptedfilename); filetransferproxy.sendupload(encryptedfilename); } [testmethod, testcategory("bvt")] public void theencryptedfileissent() { var stream = new memorystream(); var filename = fixture.create<string>(); var encryptedfilename = fixture.create<string>(); filenamebuilder .buildencryptedfilename(arg.any<string>()) .returns(encryptedfilename); sut.senduploadtoprocessor(stream, filename); // here verify fileencrypter.encrypt() gets called first filetransferproxy .received() .sendupload(encryptedfilename); }
try received.inorder
in nsubstitute.experimental
namespace.
something (i haven't tested this):
received.inorder(() => { fileencrypter.encrypt(stream, filename, encryptedfilename); filetransferproxy.sendupload(encryptedfilename); });
if you're not comfortable relying on experimental functionality, need set callbacks store calls in order, assert on that.
var calls = new list<string>(); //or enum different calls fileencrypter.when(x => x.encrypt(stream, filename, encryptedfilename)) .do(x => calls.add("encrypt")); filetransferproxy.when(x => x.sendupload(encryptedfilename)) .do(x => calls.add("upload")); // act // assert calls contains "encrypt","upload" in correct order
if end trying received.inorder
, please leave feedback on discussion group. if feedback working others can promote core namespace.
Comments
Post a Comment