asiftasleem / nbuilder

Automatically exported from code.google.com/p/nbuilder
0 stars 0 forks source link

Requested feature: Let NBuilder be able to create test objects by using immutable and private fields/properties #44

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I found NBuilder through a link in the comments in the article "Test Data
Builders: an alternative to the Object Mother pattern" at this link:
http://www.natpryce.com/articles/000714.html
Then there was a comment back saying that the objects NBuilder creates are
having mutable state.

Since those coments were a year old, I thought that the library might have
been improved since then, but I could actually not figure out how to tell
NBuilder to modify immutable and private readonly properties/fields for the
test builder creation of test objects.
If this actually is possible, I would like to see an example but if not,
consider this as a feature request.

If you think it is tricky to write that kind of reflection code yourself,
you might want to use the fasterflect library:
http://fasterflect.codeplex.com/

Code example illustrating how to modify some private and readonly
fields/properties with fasterflect:

using Fasterflect;
...
        [TestMethod]
        public void
VerifyModificationOfPrivatePropertyAndPrivateReadOnlyField()
        {
            Type type = typeof (MyClass);
            object o = type.CreateInstance();
            o.SetPropertyValue("PropertyWithPrivateSetter", "Some string");
            o.SetFieldValue("_readOnlyIntegerProperty", 123);

            MyClass myClass = (MyClass) o;

            Assert.AreEqual("Some string", myClass.PropertyWithPrivateSetter);
            Assert.AreEqual(123, myClass.ReadOnlyInteger);
        }
... 
    public sealed class MyClass {
        private MyClass() {}

        public MyClass(string someString, int someInteger) {
            PropertyWithPrivateSetter = someString;
            _readOnlyInteger = someInteger;
        }

        public string PropertyWithPrivateSetter { get; private set; }

        private readonly int _readOnlyInteger;
        public int ReadOnlyInteger { 
            get {
                return _readOnlyInteger;
            }
        }
    }

In the example above, I am using strings to specify the names of the
properties, but for properties with a public getter it should be possible
to provide a refactoring friendly API with lambda expresions, even though
the fasterflect library does not currently seem to expose that kind of API
for you (at least not as far as I can tell).

Regarding the private fields, which can not be referred to directly from
the client code using NBuilder, you might define an API similar to Fluent
NHibernate, which lets you choose a convention, such as specifying that the
field is named with an underscore with the same name as a public getter.

For example, Fluent NHibernate uses this kind of syntax:
Map(x => x.Title).Access.CamelCaseField(Prefix.Underscore);
when the class has a "private string _title" with a corresponding a "public
string Title get { return _title; }"
(
http://stackoverflow.com/questions/2041712/unit-test-fluent-nhibernate-ordered-l
ist-mapping
)

/ Tomas

Original issue reported on code.google.com by dddsver...@oo-systemutvecklare.se on 27 Mar 2010 at 4:59

GoogleCodeExporter commented 8 years ago
I have added this feature in response to the patch provided in issue 50. You 
can see the changes that have been made at revision 69 and revision 70.

the syntax is as follows:
 var invoice = Builder<Invoice>
                .CreateNew()
                .With(x => x.Amount, 100)
                .And(x => x.Id, 200)
                .Build();

Let me know if you are happy with the way this feature has been implemented.

Original comment by joshuajr...@gmail.com on 14 Sep 2010 at 7:54

GoogleCodeExporter commented 8 years ago

Original comment by joshuajr...@gmail.com on 14 Sep 2010 at 7:56

GoogleCodeExporter commented 8 years ago

Original comment by joshuajr...@gmail.com on 14 Sep 2010 at 7:59

GoogleCodeExporter commented 8 years ago
Just realised that we hadn't thought about adding support for constructing 
lists of immutable classes. As such I've added support for the Have, Has, and 
And methods in revision 73. Let me know if you're happy with this change.

Original comment by joshuajr...@gmail.com on 14 Sep 2010 at 11:52

GoogleCodeExporter commented 8 years ago
Hey Gareth,

Can you please verify that you're happy for this change to go into the release.

Thanks,
Josh

Original comment by joshuajr...@gmail.com on 21 Oct 2010 at 11:49

GoogleCodeExporter commented 8 years ago
i don't want nbuilder to be able to do this. i've added a github task to remove 
this functionality altogether.

Original comment by garethdo...@googlemail.com on 1 Feb 2013 at 4:53