devlooped / moq

The most popular and friendly mocking framework for .NET
Other
5.94k stars 802 forks source link

Cannot cast inside the Action<T> parameter of Verify method #1438

Open BlueHydrangea2908 opened 1 year ago

BlueHydrangea2908 commented 1 year ago

I met an exception when trying to run the test that have the following code. When I try to make an expression that using a cast, this exception was throw by Moq: "System.ArgumentException : Unsupported expression: (IObserver)player". Why Moq don't allow to cast in this situation? Or I'm writing the test code in wrong way?

collectionOfMockForPlayer[1]
    .Verify((IPlayer player) => ((IObserver<IMove>)player).OnError(It.IsAny<MatchManager.IllegalMoveException>()), Times.Once());

Back this issue Back this issue

github-actions[bot] commented 2 months ago

Due to lack of recent activity, this issue has been labeled as 'stale'. It will be closed if no further activity occurs within 30 more days. Any new comment will remove the label.

kzu commented 2 months ago

Verify is used to match against invocations on the mock. Casting the object to another type does not involve any invocations on it (by default, unless you're trying to test an override of an implicit or explicit cast). Is that your scenario?