Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non virtual methods can not be intercepted

I am new to FakeItEasy and try solve a problem.

I have a class

 public class Events
 {
 public List<Events> SaveEvents()
 {
 // Call to repository and return 1(success) or -1(fail)
 //If the repository return 1 then need to make another call to save the action in db
 //Sample Code here
   AuditLogService log = new AuditLogService();
   log.CallLog();
 }
 }

Here is the Test Code:

    [TestMethod]
    public void EventValidation()
    {
        //Arrange           

         var Fakeevents = A.Fake<Events>();
         var log = A.Fake<AuditLogService>();
         var _EventsController = new EventsController(Fakeevents);
        _EventsController.SaveEvents();
        A.CallTo(
             () => Fakeevents.SaveEvents().Retunr(1).AssignsOutAndRefParameters(status)
         A.CallTo(
             () => log.CallLog()).MustHaveHappened(Repeated.AtLeast.Once);
    } 
 I am getting error like "Non virtual methods can not be intercepted"

I want to check whether the Calllog method is called after success or not.

Can anyone please help me on this.

I have a method and inside a method i am initiating another class and calling a method of the class. I want to check from fakeItEasy whether the method is called.

like image 460
user2181707 Avatar asked Oct 15 '14 02:10

user2181707


People also ask

Can a non-virtual function be overridden?

By default, methods are non-virtual, and they cannot be overridden. Virtual modifiers cannot be used with static, abstract, private, and override modifiers.

Can non-virtual methods be overridden C#?

You cannot override a non-virtual or static method. The overridden base method must be virtual , abstract , or override . An override declaration cannot change the accessibility of the virtual method.

What is non-virtual method?

Methods that don't have either the virtual or override keywords, or that have the new keyword, are said to be non-virtual. When a virtual method is invoked on an object, the run-time type of the object is used to determine which implementation of the method to use.

Can we override non-virtual method in Java?

You can override any method of a class, the question is really how to access the non-virtual method, or should I have made the method virtual in the first place. With a virtual method, you always access the overridden method regardless of the class variable used to reference it.


1 Answers

Unfortunately, your title says it all. Non-virtual members cannot be faked, configured, or intercepted, as noted in the documentation under "What members can be overridden?".

There's nothing that FakeItEasy can do for you unless you make the member virtual (or promote it to an interface and fake the interface, or something similar).

like image 65
Blair Conrad Avatar answered Oct 02 '22 09:10

Blair Conrad