Search code examples
c#unit-testingmockingmoqnmock2

Convert NMock2 test to Moq


I'm new to unit testing and mocking. I have to convert unit tests in my current project with using Moq. Currently tests are using Nmock2. Can you help me with converting this code (using of CollectAction) with using Moq:

Action<IScanFolder> publish;
Mockery mocks = new Mockery();
this.mockChannel= mocks.NewMock<IChannel>();
...
CollectAction collect = new CollectAction(1);
Expect.Once.On(mockChannel).Method("Subscribe").
    With(p1, NMock2.Is.NotNull).
    Will(collect);

...

mocks.VerifyAllExpectationsHaveBeenMet();
publish = collect.Parameter as Action<ISomeInterface>;

Thanks in advance.


Solution

  • Callback is Moq's CollectAction:

    Action<ISomeInterface> publish;    
    mockChannel.Setup(c => c.Subscribe(p1, It.IsAny<TArg2>())).Callback((arg1, arg2) => publish = arg2)