c# - Mocking Method Execution Times and Sequence -


i using moq paired interface of methods. need test methods in interface executed in sequence number of times each one.

interface

public interface iinterface {     void methodone(string foo);     void methodtwo(string foo); } 

method

// myclass stuff ...  public async task run() {     methodone("foo");     methodtwo("foo"); }  // ... 

tests

i've written test verify methods executed amount of times (once):

[testmethod] public async task test() {     var mock = new mock<iinterface>();     var mocksequence = new mocksequence();      var obj = new myclass();     await obj.run();      mock.verify(i=> i.methodone("foo"), times.once());     mock.verify(i=> i.methodtwo("foo"), times.once()); } 

this works fine...

i've tried these tests determining sequence met, test seems pass.

[testmethod] public async task test() {     var mock = new mock<iinterface>();     var mocksequence = new mocksequence();      var obj = new myclass();     await obj.run();      mock.insequence(mocksequence).setup(i => i.methodone("foo"));     mock.insequence(mocksequence).setup(i => i.methodtwo("foo")); } 

should pass, , does...

[testmethod] public async task test() {     var mock = new mock<iinterface>();     var mocksequence = new mocksequence();      var obj = new myclass();     await obj.run();      mock.insequence(mocksequence).setup(i => i.methodtwo("foo")); // swapped order here     mock.insequence(mocksequence).setup(i => i.methodone("foo")); } 

should not pass, does...

  1. what need differently verify proper sequence met?
  2. how combine 2 can test number of execution times , proper sequence?

this may further off topic want go nsubstitute great mocking library handles well. in nsubstitute it's just:

  var mock = substitute.for<iinterface>();   var obj = new myclass();   await obj.run();    received.inorder(() => {     mock.methodone("foo");     mock.methodtwo("foo");     }); 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -