1

I need to mock a method from the google calendar api. But it gives an error everytime. I have the following code.

        DateTime dateTimeNow = new DateTime(2018, 10, 10, 10, 10, 10);
        Event events = new Event
        {
            Id = "EventA",
            Summary = "Summary",
            Description = "Desc",
            Start = new EventDateTime { Date = "2018-10-10", DateTime = dateTimeNow},
            End = new EventDateTime { Date = "2018-10-10", DateTime = dateTimeNow.AddHours(1) },
        };

        Mock<CalendarService> fakeCalendarService = new Mock<CalendarService>();
        fakeCalendarService
            .Setup(x => x.Events.Insert(It.IsAny<Event>(), It.IsAny<string>()).Execute())
            .Returns(events);

I get the following error message:

Message: System.NotSupportedException : Invalid setup on a non-virtual (overridable in VB) member: x => x.Events.Insert(It.IsAny<Event>(), It.IsAny<String>()).Execute()

I was able to mock other functions but this function:

_service.Events.Insert(newEvent, _calendarId).Execute();

has .Execute() behind it. Because of this statement I don't know how to mock it.

  • 2
    According to error message, You are trying to mock a non virtual member which the mocking framework is unable to mock. encapsulate the 3rd party dependency behind an abstraction you control that way you can mock it easily. – Nkosi Jun 17 '18 at 19:33
  • 1
    You have to understand how mocking works. Usually this is done by creating a class deriving from your class that contains the method and override its members. But as you can only override virtual members, the mcking-framework isn´t able to do so for non-virtual members. – MakePeaceGreatAgain Jun 17 '18 at 19:39

0 Answers0