New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Request: Add Clone Method to IFixture #1200
Comments
@ajorians Good day! From how I understood your example, it sounds like we already have what you need. The Review this playground demo: public class Model
{
public int Id { get; set; }
public string Name { get; set; }
}
[Fact]
public void Demo()
{
var fixture = new Fixture();
fixture.Customize<Model>(c => c.With(m => m.Id, 42));
var value1 = fixture.Create<Model>();
Assert.Equal(42, value1.Id);
Assert.NotNull(value1.Name);
var value2 = fixture.Build<Model>()
.With(m => m.Id, (int genInt) => genInt * 2 /* make it always even */)
.Without(x => x.Name)
.Create();
Assert.NotEqual(42, value2.Id);
Assert.Equal(value2.Id % 2, 0);
Assert.Null(value2.Name);
var value3 = fixture.Create<Model>();
Assert.Equal(42, value3.Id);
Assert.NotNull(value3.Name);
} Let us know whether it solves your needs. If not, please describe more specifically why it doesn't work for you, so we could try helping. Thanks! |
Hi @zvirja , Good day! If I may; our public class Model
{
public Model( int id, string name )
{
Id = id;
Name = name;
}
public int Id { get; }
public string Name { get; }
} Sorry for not specifying that originally. So as I understand things I have to add customizations in order to do constructor parameters. And these customizations are before the If interested some of the reasons we do a lot in the constructor and without setters is so we can use the Hope that makes better sense. Thanks for helping with this! 😃 |
@ajorians Do I understand your problem correctly, that you have a model with large amount input arguments, so you would like to customize just one of it, without having to explicitly worry about it. Right? Otherwise, you can always write something like var value2 = fixture.Build<Model>()
.FromFactory((int id, string name) => new Model(id, name))
.Create(); Unfortunately, it does not scale pretty good with growing number of parameters 😟 |
Yes, that is exactly right.
Luckily And will probably grow an additional I get this may be unusual. But yes I have a large amount of growing input constructor arguments and I would like to customize just one (or a few) of them without thinking about the rest of them. Without having that type registered or frozen for the whole test. And in a concise way as adding a customization is about I luckily am able to do all of that; but the way I did it involved making a Hope this all makes sense. Thanks for looking at this with me 😃 |
@ajorians I was about to close the issue due to inactivity but I could not walk by that gigantic constructor. By looking at your constructor I can see several issues and potential solutions to your problem. First of all you are mixing the concepts of newable and injectable entities. Second is addressing the over-injection code smell. Since the class is called Let me know if this helps. |
Hi @aivascu ,
If there is anything I can do just let me know. Being a feature request I can see until implemented it might have quite a fair amount of inactivity.
At the moment we are considering becoming a prism application. That would help with some of the items you mentioned. But sadly I don't see us changing the philosophy of just put everything in the constructor such that it can be checked for
All of this does help. I'll let me team know that even people in the world think we need to consider making changes. But if I may ask is it reasonable for Thanks for reading and considering 😃 |
@ajorians my opinion is that we should not add a There have been a few requests to implement an unfreeze/eject/freeze-once feature in the past but no one got to implement it. What you can do is implement a relay that ignores the requests after a certain count of successful resolutions, like below. You can also update the implementation to skip a certain amount of requests. The rest should be covered by the customization you used in the original post. public class CountingRelay : ISpecimenBuilder
{
public CountingRelay(ISpecimenBuilder builder, int maxCount)
{
this.MaxCount = maxCount;
this.Builder = builder;
}
public int Count { get; private set; }
public int MaxCount { get; }
public ISpecimenBuilder Builder { get; }
public object Create(object request, ISpecimenContext context)
{
if (this.Count == this.MaxCount) return new NoSpecimen();
var result = this.Builder.Create(request, context);
if (!(result is NoSpecimen)) this.Count++;
return result;
}
} |
Introduction
I would like if
IFixture
had aClone
method or something similar to create a copy of the fixture.At the moment I can create an extension method to do this:
But I feel such an ability to make a copy ought to be in the project itself.
Details
My scenario is I construct a couple instances of a class with a lot of parameters in a test. In addition I want to have a constructor parameter be a certain value for one instance and a different constructor value for a different instance. There are several ways I can do this; the way I have been doing it is:
The reason for the removal of the customization is I tried without it thinking maybe the last customization added will have a higher precedence and be used; but it wasn't the case.
If I try to simplify this with some sort of extension method like so:
It might work in some places but won't in others because after the object is built the fixture contains a customization I no longer desire and I need to remove.
I would prefer not to have a line to remove the added customization(s). So if instead my extension method made a copy of my fixture to which I add modifications to the copy then my creations works as expected and the original fixture is untouched.
Also would work is the ability to add a customization that is automatically removed after the object is created.
Hope this all makes sense.
Thanks for considering my issue 😃
The text was updated successfully, but these errors were encountered: