Sunday, 15 July 2012

c# - Moq verify that the same method is called with different arguments in specified order -



c# - Moq verify that the same method is called with different arguments in specified order -

here's method need unit test:

void do(ienumerable<string> items, textwriter tw){ foreach(var item in items) { tw.writeline(item); } }

how can configure mock of textwriter verify arguments passed same method (writeline) in order ?

[test] public void test(){ var mock = new mock<textwriter>(); mock.setup( ??? ); //check writeline called 3 times, //with arguments "aa","bb","cc", in order. do(new[]{"aa", "bb", "cc"}, mock); mock.verify(); }

you can utilize callback verify passed parameter each call:

[test] public void test(){ var arguments = new[]{"aa", "bb", "cc"}; var mock = new mock<textwriter>(); int index = 0; mock.setup(tw => tw.writeline(it.isany<string>())) .callback((string s) => assert.that(s, is.equalto(arguments[index++]))); do(arguments, mock.object); mock.verify(); // check arguments passed assert.that(index, is.equalto(arguments.length)); }

c# unit-testing moq

No comments:

Post a Comment