Non virtual methods can not be intercepted
Asked Answered
Z

3

9

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.

Zimmer answered 15/10, 2014 at 2:43 Comment(0)
T
8

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).

Traipse answered 15/10, 2014 at 9:50 Comment(1)
var log = A.Fake<AuditLogService>(); use interface instead of AuditLogService. And have this class implement that interface.Molton
P
0

Have you tried to use function? Like this:

Func<YourReturnType> action = () => YourMethod(params); // Act

action.Should().Throw<Exception>(); // Assert
Proud answered 14/12, 2021 at 11:26 Comment(0)
M
0

var log = A.Fake();

Use interface instead of AuditLogService. And have this class implement that interface

var log = A.Fake();

Molton answered 30/6, 2022 at 17:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.