2

I'm trying to setup a method on a Mocked object that is async.

As the object is not crucial to my actual test, i just want the method to return an integer, which is its return type.

My code is below :-

_legacyUnitOfWorkMock.Setup(x => x.CommitAsync()).Returns(Task.FromResult<int>(1));

However, I get the following error message :-

System.NotSupportedException : Invalid setup on a non-virtual (overridable in VB) member: x => x.CommitAsync()

Can anyone point out why this code would fail?

Derek
  • 8,300
  • 12
  • 56
  • 88
  • 1
    Can you post more about what _legacyUnitOfWorkMock is, In particular the CommitAsync signature? – NikolaiDante Jan 12 '16 at 11:50
  • 3
    When the method is not [virtual](http://stackoverflow.com/questions/1962010/moq-invalid-setup-on-a-non-overridable-member-x-x-getbytitleasdf), then it can't be mocked with Moq. – Daniel Dušek Jan 12 '16 at 11:56

1 Answers1

4

Have you tried:

_legacyUnitOfWorkMock.Setup(x => x.CommitAsync()).ReturnsAsync(1);

Or

_legacyUnitOfWorkMock.Setup(x => x.CommitAsync()).Returns(Task.FromResult(1));
Pedro Benevides
  • 1,970
  • 18
  • 19