question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature Request] Add a `WithFactoryMethod` option to IFakeOptions

See original GitHub issue

Currently, I have classes with a factory creation method instead of a public constructor and I can’t fake them.

What would you guys think of a new method to handle the creation with the factory method vs a constructor?

For example, say I have a class like this.

public class Recipe : BaseEntity
{
    public string Title { get; private set; }
    public string Directions { get; private set; }
    public int? Rating { get; private set; }
    public Author Author { get; private set; }
    public ICollection<Ingredient> Ingredients { get; private set; }

    public static Recipe Create(RecipeForCreationDto recipeForCreationDto)
    {
        new RecipeForCreationDtoValidator().ValidateAndThrow(recipeForCreationDto);
        var mapper = new Mapper(new MapperConfiguration(cfg => {
            cfg.AddProfile<RecipeProfile>();
        }));
        var newRecipe = mapper.Map<Recipe>(recipeForCreationDto);
        newRecipe.QueueDomainEvent(new RecipeCreated(){ Recipe = newRecipe });
        
        return newRecipe;
    }
        
    public void Update(RecipeForUpdateDto recipeForUpdateDto)
    {
        new RecipeForUpdateDtoValidator().ValidateAndThrow(recipeForUpdateDto);
        var mapper = new Mapper(new MapperConfiguration(cfg => {
            cfg.AddProfile<RecipeProfile>();
        }));
        mapper.Map(recipeForUpdateDto, this);
        QueueDomainEvent(new RecipeUpdated(){ Recipe = this });
    }
    
    private Recipe() { } // For EF
}

I could then create a fake for it it like this:

RecipeForCreationDto fakeDto = new FakeRecipeForCreationDto();
var fakeTracking = A.Fake<Reccipe>(x => 
            x.WithFactoryMethod(x => x.Create(fakeDto)));

Totally open to other APIs, but this is a gap in the current API that is making things hard for this use case.

Happy to make a contribution if you guys aren’t up for it, but I haven’t looked at your codebase at all yet, so any guidance would be appreciated!

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
pdevito3commented, May 6, 2022

Awesome. Thanks for the help!

0reactions
blairconradcommented, May 6, 2022

No, Moq and NSubstitute would have about the same powers as FakeItEasy. We all build on the same base.

I was thinking of something like Isolator, which I used a very long time ago, and I think there’s another. Oh! JustMock. I’ve never used that one. There may be others…

Read more comments on GitHub >

github_iconTop Results From Across the Web

allow developers to extend or change the options
I see three possible extensions: 1. Allow developers to add their own options to the admin interface. 2. Allow developers to disable/enable ...
Read more >
(Feature request) Add 'Expandable' option to Include Note ...
Describe feature Currently there are three options when 'including' notes: It would be nice if there was one more option called something like...
Read more >
🚀 Feature Request: · Issue #19620 · elementor ...
I would like to suggest a feature that would be very useful... Add an option in the popup to open the popup already...
Read more >
Feature Request Content
This page covers the content found in a feature request for your iOS app. Suggest Edits. Users can add new feature requests or...
Read more >
Showing the Feature Requests List
Described here is how to invoke the list of feature requests inside your React ... new feature requests, add comments, and up-vote other...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found