Skip to content
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

Callback after an object is created #988

Open
Kralizek opened this issue Feb 28, 2018 · 2 comments
Open

Callback after an object is created #988

Kralizek opened this issue Feb 28, 2018 · 2 comments
Labels
enhancement feature request An idea for a change, aimed at impoving quality of life
Milestone

Comments

@Kralizek
Copy link
Contributor

Kralizek commented Feb 28, 2018

I got a class with some properties and a dictionary that contains the value of each property. I'm trying to use a customization to get that nasty dictionary populated with the right values, but the "Do" method gets invoked before the properties are valued. So I got a dictionary with "items" but the items have no value :(

The problem I am experiencing is that "Do" is executed during the object creation, when the properties are not yet populated.

Ideally I would love to have a method that gets executed after the object is created. Something like OnCreate<T>(Action<T>).

This is a sample of class I want get from AutoFixture.

public class Contact : IHubSpotEntity
{
    [DefaultProperty("vid")]
    public long Id { get; set; }

    [CustomProperty("firstname")]
    public string FirstName { get; set; }

    ...

    IReadOnlyDictionary<string, object> IHubSpotEntity.Properties { get; set; } = new Dictionary<string, object>();
}

Here is my attempt to solve the issue with Do<T>(Action<T>)

IFixture fixture = new Fixture();

fixture.Customize<Contact>(c => c.Do(contact =>
{
	((IHubSpotEntity)contact).Properties = new Dictionary<string, object>
	{
		["firstname"] = contact.FirstName,
		["lastname"] = contact.LastName,
		["email"] = contact.Email,
		["createdate"] = contact.Created,
		["associatedcompanyId"] = contact.AssociatedCompanyId
	};
}));

return fixture;

And here is a simple unit test that shows my intended result

[Test, ContactAutoData]
public void Customization_works(Contact contact)
{
    IHubSpotEntity hubSpot = contact;

    Assert.That(contact.FirstName, Is.EqualTo(hubSpot.Properties["firstname"])); // BOOM!
}

where ContactAutoData is a specialization of AutoDataAttribute of AutoFixture.NUnit3 fed with the fixture customized above.

@zvirja
Copy link
Member

zvirja commented Mar 4, 2018

Hi @Kralizek, thanks for raising this issue.

Unfortunately, currently API doesn't allow you to register callback that is executed after the properties are assigned. It's unlikely that you could somehow work around that using the Customize<> API.

I'm registering that as a potential feature, as current API features are not enough for scenario like this. I do understand that we should not cover all the potential usages, however current scenario looks fundamental.

@Fed03
Copy link

Fed03 commented Oct 4, 2021

I believe this is one of the most useful functionality that are currently missing in an otherwise awesome pkg!

@aivascu aivascu added the feature request An idea for a change, aimed at impoving quality of life label Dec 5, 2023
@aivascu aivascu added this to the future milestone Dec 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement feature request An idea for a change, aimed at impoving quality of life
Projects
None yet
Development

No branches or pull requests

4 participants