I have a test (code below) to verify that Method1 calls method2. The exception that I get is
The current proxy generator cannot intercept the specified method for the following reason: - Sealed methods cannot be intercepted.
The test method does not seal itself. However, it has a dependency on a sealed class (a third-party class for which I have problems creating a wrapper to mock it - is another topic for another question). In any case, at the moment I do not ask FakeItEasy to mock the sealed class. And when debugging my test, when a dependency is called, I can clearly see that a real object is being created, not a fake.
And yet, given the error message, I feel this might be related in some way.
In addition, I discovered through a random blog post that makes the method virtual, fixes the problem, allowing me to check the pass. I tried and it worked. But I donβt understand why he fixed it, and despite this, it makes no sense for me to keep the method virtual. In my case, the test class does not have its own children, i.e. there are no children to override its methods, so I see no reason to make it virtual.
Am I mistaken in thinking that I have no reason to make this method virtual?
Is FakeItEasy really trying to mock this private class?
I am really not sure how to continue this test.
My test
[SetUp] public void SetUp() { // Arrange _service2 = A.Fake<Service2>(x => x.WithArgumentsForConstructor( () => new Service2())); _service1 = A.Fake<Service1>(x => x.WithArgumentsForConstructor( () => new Service1(_service2))); } [Test] public void when_Method1_executes_it_calls_Method2() { // Act result = _service1.Method1(); // Assert A.CallTo(() => _service2.Method2()) .WithAnyArguments() .MustHaveHappened(); }
Related Methods
public class Service1 : IService1 { private readonly IService2 _service2; public Service1(IService2 service2) { _service2 = service2; } public bool Method1() { using (var dependency = new MyDependency())
c # asp.net-mvc testing fakeiteasy
Ef0
source share