AutoFixture: How to set (nested) properties manually based on a pattern
I have the following nested classes, that are coming from XSD files generated via xsd.exe.
public class MyClass
{
public AnotherClass PropertyOne;
public DateTime PropertyTwo;
public bool PropertyTwoSpecified
}
public class AnotherClass
{
public DateTime AnotherPropertyOne
public bool AnotherPropertyOneSpecified
public int AnotherPropertyTwo
public bool AnotherPropertyTwoSpecified
}
Now I would like to use AutoFixture to generate instances with synthetic data.
var fixture = new Fixture();
fixture.Customize(new AutoFakeItEasyCustomization());
var myClassFake = fixture.Create<MyClass>();
I know I can use .with
to set single properties, but how can I set properties based on a specific pattern? Especially when this properties are nested in arrays?
I basically have to ensure that all properties ending with *Specified
are getting set to true
. Including the once nested into PropertyOne
Do I have to use my one reflection based method, e.g. an extension method (e.g. myClassFake.EnableAllProperties()
) or is there an AutoFixture-way to achieve my goal?
I know I can use fixture.Register<bool>(() => true);
to set all my bools to true. This solves my very specific problem, but still feels clumsy and not generally applicable. Still looking for the precise way to solve this.
c# tdd system.reflection autofixture
add a comment |
I have the following nested classes, that are coming from XSD files generated via xsd.exe.
public class MyClass
{
public AnotherClass PropertyOne;
public DateTime PropertyTwo;
public bool PropertyTwoSpecified
}
public class AnotherClass
{
public DateTime AnotherPropertyOne
public bool AnotherPropertyOneSpecified
public int AnotherPropertyTwo
public bool AnotherPropertyTwoSpecified
}
Now I would like to use AutoFixture to generate instances with synthetic data.
var fixture = new Fixture();
fixture.Customize(new AutoFakeItEasyCustomization());
var myClassFake = fixture.Create<MyClass>();
I know I can use .with
to set single properties, but how can I set properties based on a specific pattern? Especially when this properties are nested in arrays?
I basically have to ensure that all properties ending with *Specified
are getting set to true
. Including the once nested into PropertyOne
Do I have to use my one reflection based method, e.g. an extension method (e.g. myClassFake.EnableAllProperties()
) or is there an AutoFixture-way to achieve my goal?
I know I can use fixture.Register<bool>(() => true);
to set all my bools to true. This solves my very specific problem, but still feels clumsy and not generally applicable. Still looking for the precise way to solve this.
c# tdd system.reflection autofixture
1
That dupe hammer of mine bothers me a bit; I'd much rather like to vote to close this as a duplicate. If the other post doesn't answer your question, though, please let me know, and I'll reopen this question again.
– Mark Seemann
yesterday
Unfortunately the linked post does not answer my question. I am trying to set a specific set of properties (based on a pattern) to a specific value. E.g. setting all properties ending with*Specified
totrue
or setting all (nested) properties which name isOID
to a specific value/generation algorithm. I hope its somehow clear what I am trying to achieve. Thanks in advance
– Matze
10 hours ago
TheReflectionVisitor
used in stackoverflow.com/a/47167338/126014 ought to be useful for that task as well, but I don't mind reopening the question...
– Mark Seemann
10 hours ago
add a comment |
I have the following nested classes, that are coming from XSD files generated via xsd.exe.
public class MyClass
{
public AnotherClass PropertyOne;
public DateTime PropertyTwo;
public bool PropertyTwoSpecified
}
public class AnotherClass
{
public DateTime AnotherPropertyOne
public bool AnotherPropertyOneSpecified
public int AnotherPropertyTwo
public bool AnotherPropertyTwoSpecified
}
Now I would like to use AutoFixture to generate instances with synthetic data.
var fixture = new Fixture();
fixture.Customize(new AutoFakeItEasyCustomization());
var myClassFake = fixture.Create<MyClass>();
I know I can use .with
to set single properties, but how can I set properties based on a specific pattern? Especially when this properties are nested in arrays?
I basically have to ensure that all properties ending with *Specified
are getting set to true
. Including the once nested into PropertyOne
Do I have to use my one reflection based method, e.g. an extension method (e.g. myClassFake.EnableAllProperties()
) or is there an AutoFixture-way to achieve my goal?
I know I can use fixture.Register<bool>(() => true);
to set all my bools to true. This solves my very specific problem, but still feels clumsy and not generally applicable. Still looking for the precise way to solve this.
c# tdd system.reflection autofixture
I have the following nested classes, that are coming from XSD files generated via xsd.exe.
public class MyClass
{
public AnotherClass PropertyOne;
public DateTime PropertyTwo;
public bool PropertyTwoSpecified
}
public class AnotherClass
{
public DateTime AnotherPropertyOne
public bool AnotherPropertyOneSpecified
public int AnotherPropertyTwo
public bool AnotherPropertyTwoSpecified
}
Now I would like to use AutoFixture to generate instances with synthetic data.
var fixture = new Fixture();
fixture.Customize(new AutoFakeItEasyCustomization());
var myClassFake = fixture.Create<MyClass>();
I know I can use .with
to set single properties, but how can I set properties based on a specific pattern? Especially when this properties are nested in arrays?
I basically have to ensure that all properties ending with *Specified
are getting set to true
. Including the once nested into PropertyOne
Do I have to use my one reflection based method, e.g. an extension method (e.g. myClassFake.EnableAllProperties()
) or is there an AutoFixture-way to achieve my goal?
I know I can use fixture.Register<bool>(() => true);
to set all my bools to true. This solves my very specific problem, but still feels clumsy and not generally applicable. Still looking for the precise way to solve this.
c# tdd system.reflection autofixture
c# tdd system.reflection autofixture
edited yesterday
asked yesterday
Matze
495929
495929
1
That dupe hammer of mine bothers me a bit; I'd much rather like to vote to close this as a duplicate. If the other post doesn't answer your question, though, please let me know, and I'll reopen this question again.
– Mark Seemann
yesterday
Unfortunately the linked post does not answer my question. I am trying to set a specific set of properties (based on a pattern) to a specific value. E.g. setting all properties ending with*Specified
totrue
or setting all (nested) properties which name isOID
to a specific value/generation algorithm. I hope its somehow clear what I am trying to achieve. Thanks in advance
– Matze
10 hours ago
TheReflectionVisitor
used in stackoverflow.com/a/47167338/126014 ought to be useful for that task as well, but I don't mind reopening the question...
– Mark Seemann
10 hours ago
add a comment |
1
That dupe hammer of mine bothers me a bit; I'd much rather like to vote to close this as a duplicate. If the other post doesn't answer your question, though, please let me know, and I'll reopen this question again.
– Mark Seemann
yesterday
Unfortunately the linked post does not answer my question. I am trying to set a specific set of properties (based on a pattern) to a specific value. E.g. setting all properties ending with*Specified
totrue
or setting all (nested) properties which name isOID
to a specific value/generation algorithm. I hope its somehow clear what I am trying to achieve. Thanks in advance
– Matze
10 hours ago
TheReflectionVisitor
used in stackoverflow.com/a/47167338/126014 ought to be useful for that task as well, but I don't mind reopening the question...
– Mark Seemann
10 hours ago
1
1
That dupe hammer of mine bothers me a bit; I'd much rather like to vote to close this as a duplicate. If the other post doesn't answer your question, though, please let me know, and I'll reopen this question again.
– Mark Seemann
yesterday
That dupe hammer of mine bothers me a bit; I'd much rather like to vote to close this as a duplicate. If the other post doesn't answer your question, though, please let me know, and I'll reopen this question again.
– Mark Seemann
yesterday
Unfortunately the linked post does not answer my question. I am trying to set a specific set of properties (based on a pattern) to a specific value. E.g. setting all properties ending with
*Specified
to true
or setting all (nested) properties which name is OID
to a specific value/generation algorithm. I hope its somehow clear what I am trying to achieve. Thanks in advance– Matze
10 hours ago
Unfortunately the linked post does not answer my question. I am trying to set a specific set of properties (based on a pattern) to a specific value. E.g. setting all properties ending with
*Specified
to true
or setting all (nested) properties which name is OID
to a specific value/generation algorithm. I hope its somehow clear what I am trying to achieve. Thanks in advance– Matze
10 hours ago
The
ReflectionVisitor
used in stackoverflow.com/a/47167338/126014 ought to be useful for that task as well, but I don't mind reopening the question...– Mark Seemann
10 hours ago
The
ReflectionVisitor
used in stackoverflow.com/a/47167338/126014 ought to be useful for that task as well, but I don't mind reopening the question...– Mark Seemann
10 hours ago
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53943827%2fautofixture-how-to-set-nested-properties-manually-based-on-a-pattern%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53943827%2fautofixture-how-to-set-nested-properties-manually-based-on-a-pattern%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
That dupe hammer of mine bothers me a bit; I'd much rather like to vote to close this as a duplicate. If the other post doesn't answer your question, though, please let me know, and I'll reopen this question again.
– Mark Seemann
yesterday
Unfortunately the linked post does not answer my question. I am trying to set a specific set of properties (based on a pattern) to a specific value. E.g. setting all properties ending with
*Specified
totrue
or setting all (nested) properties which name isOID
to a specific value/generation algorithm. I hope its somehow clear what I am trying to achieve. Thanks in advance– Matze
10 hours ago
The
ReflectionVisitor
used in stackoverflow.com/a/47167338/126014 ought to be useful for that task as well, but I don't mind reopening the question...– Mark Seemann
10 hours ago