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.

Creating a dummy for a type with PRIVATE default constructor

See original GitHub issue

Currently when FakeItEasy goes to make a type with a private constructor we just get back null and no warnings / exceptions.

Here’s a test:


public interface Thing { }

public interface Foo
{
    Task<TThing> Bar<TThing>() where TThing : Thing;
}

public class FakeThing : Thing {
    private FakeThing() { }
}

[Fact]
public async Task Test1()
{
    var foo = A.Fake<Foo>();
    var bar = await foo.Bar<FakeThing>();

    Assert.NotNull(bar);
}    

Personally - I’d like to see a real dummy actually be created. Just because the constructor is private shouldn’t prevent a dummy from being created. In my app I make users define aggregate root’s as private constructor so a new developer doesn’t go and do var entity = new InvoiceAggregateRoot() so there is a real use case for creating these objects even though they’re marked private.

But the bug is IMO if this behavior is not supported it should throw a “Could not create dummy” exception - not just return null like everything is fine.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
blairconradcommented, Jun 9, 2018

@novak, the ability to configure all of a Fake’s members to return values from AutoFixture can be accessed like this:

var fixture = new Fixture().Customize(new AutoFakeItEasyCustomization { ConfigureMembers = true });

I can’t find proper user documentation for it, but you can see the API Docs on the wonderful fuget.

0reactions
blairconradcommented, Jun 9, 2018

I’m glad you’re up and running, @volak.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it possible to create an object of the class which has ...
The answer to this is both yes and no. If we are making a constructor private then we can make the object of...
Read more >
does adding a dummy parameter to constructors of a class ...
I think it violates my taste. I would code it like this: enum Unit { Centimeter = 100, Meter = 1 }; Rect(int...
Read more >
Default Constructor in Java – Class Constructor Example
In this article, we will talk about constructors, how to create our own constructors, and what default constructors are in Java.
Read more >
Should my classes have separate constructors just for unit ...
I like to write classes with two constructors: a primary constructor used in production code, and a default constructor just for unit tests....
Read more >
default constructor Line class - C++ Forum
The default argument for p is a private static member of the class called dummy. You have to define this is the file...
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