Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
How to make any instance creation a fake (FakeItEasy)?

I have this block of code that I would like to test

class SearchEngine
{
    public void Search()
    {
        var module = new SearchModule();
        module.Search();
    }
}

I have simplified it but I cannot provide a searchmodule instance as a parameter of the function Search, nor as a constructor parameter of the class SearchEngine.

Is there a way to insure that the module object will be a fakeiteasy fake when I write my unit test?

I would like to be able to do some CallTo verifications to the module object, notably that module.Search() was called when we call Search()

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Is there a way to insure that the module object will be a fakeiteasy fake when I write my unit test?

No, it's not possible, at least not with the current shape of your code. FakeItEasy can't intercept instance creation using new.

If you want to fake something, it has to be provided somehow to the system under test. The SUT can't create it itself.

I cannot provide a searchmodule instance as a parameter of the function Search, nor as a constructor parameter of the class SearchEngine

Could you inject a factory instead? Something like this:

public interface ISearchModuleFactory
{
    SearchModule Create();
}


class SearchEngine
{
    private readonly ISearchModuleFactory _searchModuleFactory;

    public SearchEngine(ISearchModuleFactory searchModuleFactory)
    {
        _searchModuleFactory = searchModuleFactory;
    }

    public void Search()
    {
        var module = _searchModuleFactory.Create();
        module.Search();
    }
}

You could then test SearchEngine like this:

// Arrange
var factory = A.Fake<ISearchModuleFactory>();
var module = A.Fake<SearchModule>();
A.CallTo(() => factory.Create()).Returns(module);
var searchEngine = new SearchEngine(factory);

// Act
searchEngine.Search();

// Assert
A.CallTo(() => module.Search()).MustHaveHappened();
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!