{"_id":"5561dd7bb40338210035f905","user":"54c837f0ab706219009e0676","version":{"_id":"553dec691a946a0d00ad6f2a","project":"553dec691a946a0d00ad6f27","__v":2,"createdAt":"2015-04-27T07:59:37.477Z","releaseDate":"2015-04-27T07:59:37.477Z","categories":["553dec691a946a0d00ad6f2b","5543585f795b590d001dc89a"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"project":"553dec691a946a0d00ad6f27","__v":5,"category":{"_id":"5543585f795b590d001dc89a","version":"553dec691a946a0d00ad6f2a","__v":9,"pages":["554358c8b7f4540d00fcef43","55435b69b7f4540d00fcef46","55435bf2795b590d001dc8a3","55435ec262b30e0d004b1706","555636f626e9bc0d0044ea81","55563946ea5e120d00188550","555639cc26e9bc0d0044ea8d","5561dd7bb40338210035f905","56bafe8ccec63e0d00f0d2eb"],"project":"553dec691a946a0d00ad6f27","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-05-01T10:41:35.279Z","from_sync":false,"order":1,"slug":"usage","title":"Usage"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-05-24T14:17:31.385Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":0,"body":"The base class of the builders (including the [generic `Builder<T>` implementation](doc:create-object-without-requiring-custom-builder-cla)) contains a number of methods that you can make use of both internally when creating a custom builder, and externally when calling methods on a builder instance.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Set\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"/// <summary>\\n/// Records the given value for the given property from {TObject} and returns the builder to allow chaining.\\n/// </summary>\\n/// <typeparam name=\\\"TValue\\\">The type of the property</typeparam>\\n/// <param name=\\\"property\\\">A lambda expression specifying the property to record a value for</param>\\n/// <param name=\\\"value\\\">The value to set the property to</param>\\n/// <returns>The builder so that other method calls can be chained</returns>\\npublic virtual TBuilder Set<TValue>(Expression<Func<TObject, TValue>> property, TValue value)\\n  \\n/// <summary>\\n/// Records a given value provider for the given property from {TObject} and returns the builder to allow chaining.\\n/// </summary>\\n/// <typeparam name=\\\"TValue\\\">The type of the property</typeparam>\\n/// <param name=\\\"property\\\">A lambda expression specifying the property to record a value for</param>\\n/// <param name=\\\"factory\\\">A method which produces instances of {TValue} for the property.</param>\\n/// <returns>The builder so that other method calls can be chained</returns>\\npublic virtual TBuilder Set<TValue>(Expression<Func<TObject, TValue>> property, Func<TValue> factory)\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nThe `Set` methods allow you to store a value internally within the builder so it can be retrieved again. This is the core part of the implementation of the [Test Data Builder](http://www.natpryce.com/articles/000714.html) pattern. Usually you would need to create a private property to hold every intermediate value you wanted to track in your test data builder, but this gets very tedious and adds a lot of noise to the builder. The `Set` methods allow you to store a type-safe value against a public property of the object you are building.\n\nWhile you won't always want to store a value that corresponds to a public property, we find that over 90% of the time this applies and so this has a huge effect to reducing noise and making it easier to quickly get up and running with a test data builder class.\n\nThere are two `Set` overloads - one that takes a value and another that takes a lambda expression. The latter is useful when you want something lazily evaluated or you are calling `Set` as part of building a [list of objects](doc:creating-lists) and want a different value for each object generated. The lambda expression takes an [anonymous value fixture](doc:anonymous-values-and-equivalence-classes) that allows you to supply an anonymous value that [shares the fixture with the builder](doc:propagating-the-anonymous-value-fixture-across-bui).\n\n**Note:** Nested property names aren't supported - whatever the last property is in the chain is the property name that will be stored against. If this is something you need feel free to [contribute](https://github.com/TestStack/TestStack.Dossier/issues/25).\n\nThe `Set` methods return the test data builder instance so they can be used for fluent chaining.\n\nHere are some examples:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// In a CustomerBuilder\\npublic CustomerBuilder WithFirstName(string firstName)\\n{\\n    return Set(x => x.FirstName, firstName);\\n}\\n\\n// As part of a Builder<T> call\\nvar student = Builder<StudentViewModel>.CreateNew()\\n    .Set(x => x.StudentId, 12884352)\\n    .Build();\\n\\n// Evaluated as part of building a list\\nvar studentNumber = 1000000;\\nvar students = Builder<StudentViewModel>.CreateListOfSize(10)\\n    .TheFirst(1).Set(x => x.FirstName, \\\"John\\\")\\n    .All().Set(x => x.StudentId, _ => studentNumber += 10)\\n    .BuildList();\\n\\n// Use the anonymous value fixture from the builder\\nvar student = Builder<StudentViewModel>.CreateNew()\\n    .Set(x => x.FirstName, any => any.FirstName())\\n    .Build();\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Has\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"/// <summary>\\n/// Returns whether or not there is currently an explicit value recorded against the given property from {TObject}.\\n/// </summary>\\n/// <typeparam name=\\\"TValue\\\">The type of the property</typeparam>\\n/// <param name=\\\"property\\\">A lambda expression specifying the property to retrieve the recorded value for</param>\\n/// <returns>Whether or not there is a recorded value for the property</returns>\\nprotected bool Has<TValue>(Expression<Func<TObject, TValue>> property)\\n  \\n/// <summary>\\n/// Returns whether or not there is currently an explicit value recorded against the given property from {TObject}.\\n/// </summary>\\n/// <param name=\\\"propertyName\\\">A string specifying the name of the property to retrieve the recorded value for</param>\\n/// <returns>Whether or not there is a recorded value for the property</returns>\\nprotected bool Has(string propertyName)\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nThe `Has` methods return whether or not an explicit value has been set using one of the `Set` methods. There is a lambda expression version that takes a property from the object being built and there is a string version that simply takes the property name.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Get / GetOrDefault\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"/// <summary>\\n/// Gets the recorded value for the given property from {TObject} or an anonymous\\n///  value if there isn't one specified.\\n/// </summary>\\n/// <typeparam name=\\\"TValue\\\">The type of the property.</typeparam>\\n/// <param name=\\\"property\\\">A lambda expression specifying the property to retrieve the recorded value for</param>\\n/// <returns>The recorded value of the property or an anonymous value for it</returns>\\npublic TValue Get<TValue>(Expression<Func<TObject, TValue>> property)\\n\\n/// <summary>\\n/// Gets the recorded value for the given property from {type} or an anonymous\\n///  value if there isn't one specified.\\n/// </summary>\\n/// <param name=\\\"type\\\">The type of the property.</param>\\n/// <param name=\\\"propertyName\\\">The property name.</param>\\n/// <returns></returns>\\npublic object Get(Type type, string propertyName)\\n\\n/// <summary>\\n/// Gets the recorded value for the given property from {TObject} or if no\\n/// value has been recorded the default value for {TValue}.\\n/// </summary>\\n/// <typeparam name=\\\"TValue\\\">The type of the property</typeparam>\\n/// <param name=\\\"property\\\">A lambda expression specifying the property to retrieve the recorded value for</param>\\n/// <returns>The recorded value of the property or teh default value for {TValue} if no value recorded</returns>\\npublic TValue GetOrDefault<TValue>(Expression<Func<TObject, TValue>> property)\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nThe `Get` methods allow you to retrieve values that have been set using the `Set` methods. There are three variants:\n\n* One that takes a lambda expression identifying a property on the object being built, that returns a strongly-typed value corresponding to that property, and if no explicit value has been set using `Set` for that property it generates an [anonymous value](doc:anonymous-values-and-equivalence-classes)\n* One that takes the type and (string) name of the property to return a value for, or if a value hasn't been explicity set against that property using `Set` then it generates an [anonymous value](doc:anonymous-values-and-equivalence-classes)\n* One that takes a lambda expression identifying a property on the object being built, that returns a strongly-typed value corresponding to that property, and if no explicit value has been set using `Set` for that property it returns the default value for that type\n\nExamples:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var builder = Builder<StudentViewModel>.CreateNew();\\n\\nbuilder.Get(x => x.FirstName); // \\\"John\\\" (or another anonymous value via Any.FirstName()\\nbuilder.Get(typeof(string), \\\"FirstName\\\"); // \\\"John\\\" (or another anonymous value via Any.FirstName()\\nbuilder.GetOrDefault(x => x.FirstName); // null\\n\\nbuilder.Set(x => x.FirstName, \\\"Joe\\\");\\nbuilder.Get(x => x.FirstName); // \\\"Joe\\\"\\nbuilder.Get(typeof(string), \\\"FirstName\\\"); // \\\"Joe\\\"\\nbuilder.GetOrDefault(x => x.FirstName); // \\\"Joe\\\"\\n\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"ListBuilder\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"/// <summary>\\n/// The list builder instance (if this is a a list builder proxy).\\n/// </summary>\\npublic ListBuilder<TObject, TBuilder> ListBuilder { get; } \",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nIf the builder instance is a [list builder proxy object](doc:creating-lists) then calling `ListBuilder` will return the List Builder instance. You would typically only need this in more advanced scenarios e.g. you had an extension method on `ListBuilder` or some other method requiring it. Ordinarily, you would convert the proxy to a list of real objects using the `BuildList` method.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"IsListBuilderProxy\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"/// <summary>\\n/// Returns whether or not the builder instance is a proxy for building a list or an actual builder instance.\\n/// </summary>\\n/// <returns>Whether or not the instance is a list builder proxy</returns>\\npublic virtual bool IsListBuilderProxy()\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nAllows you to check if the builder instance is a list builder proxy object or a real builder instance.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Any\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"/// <summary>\\n/// Generate anonymous data using this fixture - one instance per builder instance.\\n/// </summary>\\npublic AnonymousValueFixture Any { get; }\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nYou can access the [anonymous value fixture](doc:anonymous-values-and-equivalence-classes) via the `Any` property.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"BuildUsing\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"/// <summary>\\n/// Builds the object from this builder using an <see cref=\\\"IFactory\\\"/>.\\n/// </summary>\\n/// <typeparam name=\\\"TFactory\\\">The factory to use to build the object</typeparam>\\n/// <returns>The built object</returns>\\nprotected TObject BuildUsing<TFactory>()\\n    where TFactory : IFactory, new()\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nCan be called from within your custom builder to choose an [automatic construction factory](doc:build-objects-without-calling-constructor). This replaces the need to call the `new` operator yourself.\n\n**Note**: There are a few assumptions/limitations ([as documented](doc:build-objects-without-calling-constructor)) with the default construction factories that you need to be aware of, but you can create your own auto construction factories too.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"AsProxy\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"/// <summary>\\n/// Return an NSubstitute proxy object when .Build() is called rather than a real object.\\n/// </summary>\\n/// <returns>The builder so that other method calls can be chained</returns>\\npublic TBuilder AsProxy()\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nAllows you to build an NSubstitute proxy object rather than a real object. See [Creating proxy objects](doc:creating-proxy-objects).\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"AlterProxy\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"/// <summary>\\n/// Alter the proxy object just after it has been built and before it's returned from .Build().\\n/// This allows you to add any .Returns() values that are more complex than the public properties that are proxied by default.\\n/// </summary>\\n/// <param name=\\\"proxy\\\">The proxy object</param>\\nprotected virtual void AlterProxy(TObject proxy)\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nOverride it in your custom builders to allow you to set `.Returns` values on the NSubstitute proxy that gets generated if you use `AsProxy` where you need more than just return values on the public property getters (which is added by default).\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"CreateListOfSize\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"/// <summary>\\n/// Creates an list builder expression that allows you to create a list of entities.\\n/// You can call .First(x), .Last(x), etc. methods followed by chained builder method calls.\\n/// When you are done call .BuildList() to get the list of entities.\\n/// </summary>\\n/// <param name=\\\"size\\\">The size of list</param>\\n/// <returns>The list builder for a list of {TBuilder} of the specified size</returns>\\npublic static ListBuilder<TObject, TBuilder> CreateListOfSize(int size)\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nStatic method on your builder class (or the [generic class](doc:create-object-without-requiring-custom-builder-cla)) to allow you to [create a lists of objects](doc:creating-lists) very tersely.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"GetChildBuilder\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"/// <summary>\\n/// Creates (and optionally modifies) a child builder class of this builder; sharing the anonymous value fixture.\\n/// </summary>\\n/// <typeparam name=\\\"TChildObject\\\">The type of the child object being built</typeparam>\\n/// <typeparam name=\\\"TChildBuilder\\\">The type of the builder for the child object being built</typeparam>\\n/// <param name=\\\"modifier\\\">An optional modifier lambda expression with fluent builder method calls for the child builder</param>\\n/// <returns>The instance of the child builder</returns>\\nprotected virtual TChildBuilder GetChildBuilder<TChildObject, TChildBuilder>(Func<TChildBuilder, TChildBuilder> modifier = null)\\n    where TChildObject : class\\n    where TChildBuilder : TestDataBuilder<TChildObject, TChildBuilder>, new()\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nAllows you to tersely create a value to store in your builder by using another builder class (with optional configuration), while [sharing the anonymous value fixture](doc:propagating-the-anonymous-value-fixture-across-bui).\n\nExample:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"public StudentBuilder WithClass(Func<ClassBuilder, ClassBuilder> modifier = null)\\n{\\n  return Set(x => x.CurrentClass, GetChildBuilder<AcademicClass, ClassBuilder>(modifier));\\n}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]","excerpt":"","slug":"basic-builder-methods","type":"basic","title":"Basic builder methods"}

Basic builder methods


The base class of the builders (including the [generic `Builder<T>` implementation](doc:create-object-without-requiring-custom-builder-cla)) contains a number of methods that you can make use of both internally when creating a custom builder, and externally when calling methods on a builder instance. [block:api-header] { "type": "basic", "title": "Set" } [/block] [block:code] { "codes": [ { "code": "/// <summary>\n/// Records the given value for the given property from {TObject} and returns the builder to allow chaining.\n/// </summary>\n/// <typeparam name=\"TValue\">The type of the property</typeparam>\n/// <param name=\"property\">A lambda expression specifying the property to record a value for</param>\n/// <param name=\"value\">The value to set the property to</param>\n/// <returns>The builder so that other method calls can be chained</returns>\npublic virtual TBuilder Set<TValue>(Expression<Func<TObject, TValue>> property, TValue value)\n \n/// <summary>\n/// Records a given value provider for the given property from {TObject} and returns the builder to allow chaining.\n/// </summary>\n/// <typeparam name=\"TValue\">The type of the property</typeparam>\n/// <param name=\"property\">A lambda expression specifying the property to record a value for</param>\n/// <param name=\"factory\">A method which produces instances of {TValue} for the property.</param>\n/// <returns>The builder so that other method calls can be chained</returns>\npublic virtual TBuilder Set<TValue>(Expression<Func<TObject, TValue>> property, Func<TValue> factory)", "language": "csharp" } ] } [/block] The `Set` methods allow you to store a value internally within the builder so it can be retrieved again. This is the core part of the implementation of the [Test Data Builder](http://www.natpryce.com/articles/000714.html) pattern. Usually you would need to create a private property to hold every intermediate value you wanted to track in your test data builder, but this gets very tedious and adds a lot of noise to the builder. The `Set` methods allow you to store a type-safe value against a public property of the object you are building. While you won't always want to store a value that corresponds to a public property, we find that over 90% of the time this applies and so this has a huge effect to reducing noise and making it easier to quickly get up and running with a test data builder class. There are two `Set` overloads - one that takes a value and another that takes a lambda expression. The latter is useful when you want something lazily evaluated or you are calling `Set` as part of building a [list of objects](doc:creating-lists) and want a different value for each object generated. The lambda expression takes an [anonymous value fixture](doc:anonymous-values-and-equivalence-classes) that allows you to supply an anonymous value that [shares the fixture with the builder](doc:propagating-the-anonymous-value-fixture-across-bui). **Note:** Nested property names aren't supported - whatever the last property is in the chain is the property name that will be stored against. If this is something you need feel free to [contribute](https://github.com/TestStack/TestStack.Dossier/issues/25). The `Set` methods return the test data builder instance so they can be used for fluent chaining. Here are some examples: [block:code] { "codes": [ { "code": "// In a CustomerBuilder\npublic CustomerBuilder WithFirstName(string firstName)\n{\n return Set(x => x.FirstName, firstName);\n}\n\n// As part of a Builder<T> call\nvar student = Builder<StudentViewModel>.CreateNew()\n .Set(x => x.StudentId, 12884352)\n .Build();\n\n// Evaluated as part of building a list\nvar studentNumber = 1000000;\nvar students = Builder<StudentViewModel>.CreateListOfSize(10)\n .TheFirst(1).Set(x => x.FirstName, \"John\")\n .All().Set(x => x.StudentId, _ => studentNumber += 10)\n .BuildList();\n\n// Use the anonymous value fixture from the builder\nvar student = Builder<StudentViewModel>.CreateNew()\n .Set(x => x.FirstName, any => any.FirstName())\n .Build();", "language": "csharp" } ] } [/block] [block:api-header] { "type": "basic", "title": "Has" } [/block] [block:code] { "codes": [ { "code": "/// <summary>\n/// Returns whether or not there is currently an explicit value recorded against the given property from {TObject}.\n/// </summary>\n/// <typeparam name=\"TValue\">The type of the property</typeparam>\n/// <param name=\"property\">A lambda expression specifying the property to retrieve the recorded value for</param>\n/// <returns>Whether or not there is a recorded value for the property</returns>\nprotected bool Has<TValue>(Expression<Func<TObject, TValue>> property)\n \n/// <summary>\n/// Returns whether or not there is currently an explicit value recorded against the given property from {TObject}.\n/// </summary>\n/// <param name=\"propertyName\">A string specifying the name of the property to retrieve the recorded value for</param>\n/// <returns>Whether or not there is a recorded value for the property</returns>\nprotected bool Has(string propertyName)", "language": "csharp" } ] } [/block] The `Has` methods return whether or not an explicit value has been set using one of the `Set` methods. There is a lambda expression version that takes a property from the object being built and there is a string version that simply takes the property name. [block:api-header] { "type": "basic", "title": "Get / GetOrDefault" } [/block] [block:code] { "codes": [ { "code": "/// <summary>\n/// Gets the recorded value for the given property from {TObject} or an anonymous\n/// value if there isn't one specified.\n/// </summary>\n/// <typeparam name=\"TValue\">The type of the property.</typeparam>\n/// <param name=\"property\">A lambda expression specifying the property to retrieve the recorded value for</param>\n/// <returns>The recorded value of the property or an anonymous value for it</returns>\npublic TValue Get<TValue>(Expression<Func<TObject, TValue>> property)\n\n/// <summary>\n/// Gets the recorded value for the given property from {type} or an anonymous\n/// value if there isn't one specified.\n/// </summary>\n/// <param name=\"type\">The type of the property.</param>\n/// <param name=\"propertyName\">The property name.</param>\n/// <returns></returns>\npublic object Get(Type type, string propertyName)\n\n/// <summary>\n/// Gets the recorded value for the given property from {TObject} or if no\n/// value has been recorded the default value for {TValue}.\n/// </summary>\n/// <typeparam name=\"TValue\">The type of the property</typeparam>\n/// <param name=\"property\">A lambda expression specifying the property to retrieve the recorded value for</param>\n/// <returns>The recorded value of the property or teh default value for {TValue} if no value recorded</returns>\npublic TValue GetOrDefault<TValue>(Expression<Func<TObject, TValue>> property)", "language": "csharp" } ] } [/block] The `Get` methods allow you to retrieve values that have been set using the `Set` methods. There are three variants: * One that takes a lambda expression identifying a property on the object being built, that returns a strongly-typed value corresponding to that property, and if no explicit value has been set using `Set` for that property it generates an [anonymous value](doc:anonymous-values-and-equivalence-classes) * One that takes the type and (string) name of the property to return a value for, or if a value hasn't been explicity set against that property using `Set` then it generates an [anonymous value](doc:anonymous-values-and-equivalence-classes) * One that takes a lambda expression identifying a property on the object being built, that returns a strongly-typed value corresponding to that property, and if no explicit value has been set using `Set` for that property it returns the default value for that type Examples: [block:code] { "codes": [ { "code": "var builder = Builder<StudentViewModel>.CreateNew();\n\nbuilder.Get(x => x.FirstName); // \"John\" (or another anonymous value via Any.FirstName()\nbuilder.Get(typeof(string), \"FirstName\"); // \"John\" (or another anonymous value via Any.FirstName()\nbuilder.GetOrDefault(x => x.FirstName); // null\n\nbuilder.Set(x => x.FirstName, \"Joe\");\nbuilder.Get(x => x.FirstName); // \"Joe\"\nbuilder.Get(typeof(string), \"FirstName\"); // \"Joe\"\nbuilder.GetOrDefault(x => x.FirstName); // \"Joe\"\n", "language": "csharp" } ] } [/block] [block:api-header] { "type": "basic", "title": "ListBuilder" } [/block] [block:code] { "codes": [ { "code": "/// <summary>\n/// The list builder instance (if this is a a list builder proxy).\n/// </summary>\npublic ListBuilder<TObject, TBuilder> ListBuilder { get; } ", "language": "csharp" } ] } [/block] If the builder instance is a [list builder proxy object](doc:creating-lists) then calling `ListBuilder` will return the List Builder instance. You would typically only need this in more advanced scenarios e.g. you had an extension method on `ListBuilder` or some other method requiring it. Ordinarily, you would convert the proxy to a list of real objects using the `BuildList` method. [block:api-header] { "type": "basic", "title": "IsListBuilderProxy" } [/block] [block:code] { "codes": [ { "code": "/// <summary>\n/// Returns whether or not the builder instance is a proxy for building a list or an actual builder instance.\n/// </summary>\n/// <returns>Whether or not the instance is a list builder proxy</returns>\npublic virtual bool IsListBuilderProxy()", "language": "csharp" } ] } [/block] Allows you to check if the builder instance is a list builder proxy object or a real builder instance. [block:api-header] { "type": "basic", "title": "Any" } [/block] [block:code] { "codes": [ { "code": "/// <summary>\n/// Generate anonymous data using this fixture - one instance per builder instance.\n/// </summary>\npublic AnonymousValueFixture Any { get; }", "language": "csharp" } ] } [/block] You can access the [anonymous value fixture](doc:anonymous-values-and-equivalence-classes) via the `Any` property. [block:api-header] { "type": "basic", "title": "BuildUsing" } [/block] [block:code] { "codes": [ { "code": "/// <summary>\n/// Builds the object from this builder using an <see cref=\"IFactory\"/>.\n/// </summary>\n/// <typeparam name=\"TFactory\">The factory to use to build the object</typeparam>\n/// <returns>The built object</returns>\nprotected TObject BuildUsing<TFactory>()\n where TFactory : IFactory, new()", "language": "csharp" } ] } [/block] Can be called from within your custom builder to choose an [automatic construction factory](doc:build-objects-without-calling-constructor). This replaces the need to call the `new` operator yourself. **Note**: There are a few assumptions/limitations ([as documented](doc:build-objects-without-calling-constructor)) with the default construction factories that you need to be aware of, but you can create your own auto construction factories too. [block:api-header] { "type": "basic", "title": "AsProxy" } [/block] [block:code] { "codes": [ { "code": "/// <summary>\n/// Return an NSubstitute proxy object when .Build() is called rather than a real object.\n/// </summary>\n/// <returns>The builder so that other method calls can be chained</returns>\npublic TBuilder AsProxy()", "language": "csharp" } ] } [/block] Allows you to build an NSubstitute proxy object rather than a real object. See [Creating proxy objects](doc:creating-proxy-objects). [block:api-header] { "type": "basic", "title": "AlterProxy" } [/block] [block:code] { "codes": [ { "code": "/// <summary>\n/// Alter the proxy object just after it has been built and before it's returned from .Build().\n/// This allows you to add any .Returns() values that are more complex than the public properties that are proxied by default.\n/// </summary>\n/// <param name=\"proxy\">The proxy object</param>\nprotected virtual void AlterProxy(TObject proxy)", "language": "csharp" } ] } [/block] Override it in your custom builders to allow you to set `.Returns` values on the NSubstitute proxy that gets generated if you use `AsProxy` where you need more than just return values on the public property getters (which is added by default). [block:api-header] { "type": "basic", "title": "CreateListOfSize" } [/block] [block:code] { "codes": [ { "code": "/// <summary>\n/// Creates an list builder expression that allows you to create a list of entities.\n/// You can call .First(x), .Last(x), etc. methods followed by chained builder method calls.\n/// When you are done call .BuildList() to get the list of entities.\n/// </summary>\n/// <param name=\"size\">The size of list</param>\n/// <returns>The list builder for a list of {TBuilder} of the specified size</returns>\npublic static ListBuilder<TObject, TBuilder> CreateListOfSize(int size)", "language": "csharp" } ] } [/block] Static method on your builder class (or the [generic class](doc:create-object-without-requiring-custom-builder-cla)) to allow you to [create a lists of objects](doc:creating-lists) very tersely. [block:api-header] { "type": "basic", "title": "GetChildBuilder" } [/block] [block:code] { "codes": [ { "code": "/// <summary>\n/// Creates (and optionally modifies) a child builder class of this builder; sharing the anonymous value fixture.\n/// </summary>\n/// <typeparam name=\"TChildObject\">The type of the child object being built</typeparam>\n/// <typeparam name=\"TChildBuilder\">The type of the builder for the child object being built</typeparam>\n/// <param name=\"modifier\">An optional modifier lambda expression with fluent builder method calls for the child builder</param>\n/// <returns>The instance of the child builder</returns>\nprotected virtual TChildBuilder GetChildBuilder<TChildObject, TChildBuilder>(Func<TChildBuilder, TChildBuilder> modifier = null)\n where TChildObject : class\n where TChildBuilder : TestDataBuilder<TChildObject, TChildBuilder>, new()", "language": "csharp" } ] } [/block] Allows you to tersely create a value to store in your builder by using another builder class (with optional configuration), while [sharing the anonymous value fixture](doc:propagating-the-anonymous-value-fixture-across-bui). Example: [block:code] { "codes": [ { "code": "public StudentBuilder WithClass(Func<ClassBuilder, ClassBuilder> modifier = null)\n{\n return Set(x => x.CurrentClass, GetChildBuilder<AcademicClass, ClassBuilder>(modifier));\n}", "language": "csharp" } ] } [/block]