InvalidOperationException: The delegate return value should be assignable from

794 Views Asked by At

I am getting an exception on the Do Method of Rhino Mock.

System.InvalidOperationException : The delegate return value should be assignable from MediaPlayerImpl.ScreenControl.ScreenCommunicatorState

I am not very clear how to resolve it. Below is my code. Its quite to simple to understand.

var mockCommunicator = Rhino.Mocks.MockRepository.GenerateStrictMock<IScreenCommunicator>();

ManualResetEvent screenTurnedOff = new ManualResetEvent(false);

mockCommunicator.Expect(c => c.TurnScreenOff(default(ScreenCommunicatorState))).IgnoreArguments()
                                .Do(new Action(() => screenTurnedOff.Set()));


  public override ScreenCommunicatorState TurnScreenOn(ScreenCommunicatorState state)
    {
        state = state ?? new ScreenCommunicatorState { isScreenOn = false };

        if (state.isScreenOn)
        {
            // Try and Get the screen power state

            // ....
            Task<byte[]> requestTask = SendRequest(CommandSequences.GetPowerStatus);
            var isPowerOn = InterpretScreenPowerStatusMessage(requestTask);
            if (isPowerOn.Value)
            {
                //        Do nothing
            }
            else if (!isPowerOn.Value)
            {
                SendCommandSequence(CommandSequences.PowerOn);
            }
            else if (isPowerOn == null)
            {
                SendCommandSequence(CommandSequences.PowerOn);
            }




            // NO


        }

        state.isScreenOn = true;

        Task<bool?> screenTurnedOnTask = IsScreenTurnedOn();

        Func<Task<bool?>, Task> doSomeWork = TurnScreenOnAndRectifyScreenInputSource;



        Task<Task> turnScreenOnIfNecessaryTask = screenTurnedOnTask.ContinueWith(doSomeWork, TaskContinuationOptions.NotOnFaulted);

        Task unwrapped = turnScreenOnIfNecessaryTask.Unwrap();
        try
        {
            unwrapped.Wait(); // This will thrown an exception if it's faulted, which is what we want
        }
        catch (AggregateException aggregateException)
        {
            if (aggregateException.InnerExceptions.Count() == 1)
            {
                throw aggregateException.InnerExceptions[0];
            }
            else
            {
                throw;
            }
        }
        return state;
    }
1

There are 1 best solutions below

0
maxspan On

I had to use and Func Delegate in order to resolve the issue.

.Do(new Func<ScreenCommunicatorState,ScreenCommunicatorState>((id) => {
                                screenTurnedOff.Set();
                                return new ScreenCommunicatorState();
                            }))
                            .Return(new ScreenCommunicatorState());