{"__v":3,"_id":"555639cc26e9bc0d0044ea8d","category":{"__v":9,"_id":"5543585f795b590d001dc89a","pages":["554358c8b7f4540d00fcef43","55435b69b7f4540d00fcef46","55435bf2795b590d001dc8a3","55435ec262b30e0d004b1706","555636f626e9bc0d0044ea81","55563946ea5e120d00188550","555639cc26e9bc0d0044ea8d","5561dd7bb40338210035f905","56bafe8ccec63e0d00f0d2eb"],"project":"553dec691a946a0d00ad6f27","version":"553dec691a946a0d00ad6f2a","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-05-01T10:41:35.279Z","from_sync":false,"order":1,"slug":"usage","title":"Usage"},"project":"553dec691a946a0d00ad6f27","user":"54c4b05742190d0d00f5fbde","version":{"__v":2,"_id":"553dec691a946a0d00ad6f2a","project":"553dec691a946a0d00ad6f27","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"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-05-15T18:24:12.638Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":6,"body":"Within a particular instance of `AnonymousValueFixture`, which is created for every builder, any generators that return a sequence of values (e.g. unique values) will be maintained. If you want to ensure that the same anonymous value fixture is used across multiple related builders then:\n\n* Using `CreateListOfSize` will automatically propagate the anonymous value fixture across builders\n* Call the `GetChildBuilder<TChildObject, TChildBuilder>(Func<TChildBuilder, TChildBuilder> modifier = null)` method from within your custom builder, e.g.:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"public MyCustomBuilder WithSomeValue(Func<SomeBuilder, SomeBuilder> modifier = null)\\n{\\n\\treturn Set(x => x.SomeValue, GetChildBuilder<SomeObject, SomeBuilder>(modifier));\\n}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\n* If using `Builder<T>` then call the `SetUsingBuilder` method, e.g.:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// Uses Builder<T>\\nBuilder<StudentViewModel>.CreateNew()\\n\\t.SetUsingBuilder(x => x.Address)\\n\\t.Build();\\n\\t\\n// Uses Builder<T>, includes customisation\\nBuilder<StudentViewModel>.CreateNew()\\n\\t.SetUsingBuilder(x => x.Address, b => b.Set(y => y.Street, \\\"A street\\\"))\\n\\t.Build();\\n\\t\\n// Uses AddressBuilder\\nBuilder<StudentViewModel>.CreateNew()\\n\\t.SetUsingBuilder<AddressViewModel, AddressViewModelBuilder>(x => x.Address)\\n\\t.Build();\\n\\t\\n// Uses AddressBuilder, includes customisation\\nBuilder<StudentViewModel>.CreateNew()\\n\\t.SetUsingBuilder<AddressViewModel, AddressViewModelBuilder>(x => x.Address, b => b.Set(y => y.Street, \\\"A street\\\"))\\n\\t.Build();\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\n* If you want to use the anonymous value fixture to generate a value for a specific test then you can use the [Set method overload with a lambda expression](doc:basic-builder-methods), e.g.:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var student = Builder<StudentViewModel>.CreateNew()\\n    .Set(x => x.FirstName, any => any.FirstName())\\n    .Build();\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nThere is currently no way to share an anonymous value fixture across unrelated builder instances. If this is something you need please raise an issue so we can discuss your requirement.","excerpt":"","slug":"propagating-the-anonymous-value-fixture-across-bui","type":"basic","title":"Sharing anonymous value fixture"}

Sharing anonymous value fixture


Within a particular instance of `AnonymousValueFixture`, which is created for every builder, any generators that return a sequence of values (e.g. unique values) will be maintained. If you want to ensure that the same anonymous value fixture is used across multiple related builders then: * Using `CreateListOfSize` will automatically propagate the anonymous value fixture across builders * Call the `GetChildBuilder<TChildObject, TChildBuilder>(Func<TChildBuilder, TChildBuilder> modifier = null)` method from within your custom builder, e.g.: [block:code] { "codes": [ { "code": "public MyCustomBuilder WithSomeValue(Func<SomeBuilder, SomeBuilder> modifier = null)\n{\n\treturn Set(x => x.SomeValue, GetChildBuilder<SomeObject, SomeBuilder>(modifier));\n}", "language": "csharp" } ] } [/block] * If using `Builder<T>` then call the `SetUsingBuilder` method, e.g.: [block:code] { "codes": [ { "code": "// Uses Builder<T>\nBuilder<StudentViewModel>.CreateNew()\n\t.SetUsingBuilder(x => x.Address)\n\t.Build();\n\t\n// Uses Builder<T>, includes customisation\nBuilder<StudentViewModel>.CreateNew()\n\t.SetUsingBuilder(x => x.Address, b => b.Set(y => y.Street, \"A street\"))\n\t.Build();\n\t\n// Uses AddressBuilder\nBuilder<StudentViewModel>.CreateNew()\n\t.SetUsingBuilder<AddressViewModel, AddressViewModelBuilder>(x => x.Address)\n\t.Build();\n\t\n// Uses AddressBuilder, includes customisation\nBuilder<StudentViewModel>.CreateNew()\n\t.SetUsingBuilder<AddressViewModel, AddressViewModelBuilder>(x => x.Address, b => b.Set(y => y.Street, \"A street\"))\n\t.Build();", "language": "csharp" } ] } [/block] * If you want to use the anonymous value fixture to generate a value for a specific test then you can use the [Set method overload with a lambda expression](doc:basic-builder-methods), e.g.: [block:code] { "codes": [ { "code": "var student = Builder<StudentViewModel>.CreateNew()\n .Set(x => x.FirstName, any => any.FirstName())\n .Build();", "language": "csharp" } ] } [/block] There is currently no way to share an anonymous value fixture across unrelated builder instances. If this is something you need please raise an issue so we can discuss your requirement.