{"_id":"55435ec262b30e0d004b1706","user":"54c4b05742190d0d00f5fbde","__v":0,"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"},"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"},"project":"553dec691a946a0d00ad6f27","updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-05-01T11:08:50.102Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"auth":"required","params":[],"url":""},"isReference":false,"order":7,"body":"This library integrates with [NSubstitute](http://nsubstitute.github.io/) for generating proxy objects, this means you can call the `AsProxy` method on your builder to request that the result from calling `Build` will be an NSubstitute proxy with the public properties set to return the values you have specified via your builder, e.g.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var customer = CustomerBuilder.WithFirstName(\\\"Rob\\\").AsProxy().Build();\\ncustomer.CustomerForHowManyYears(Arg.Any<DateTime>()).Returns(10);\\nvar name = customer.FirstName; // \\\"Rob\\\"\\nvar years = customer.CustomerForHowManyYears(DateTime.Now); // 10\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\n    \n\nIf you need to alter the proxy before calling `Build` to add complex behaviours that can't be expressed by the default public properties returns values then you can override the `AlterProxy` method in your builder, e.g.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"class CustomerBuilder : TestDataBuilder<Customer, CustomerBuilder>\\n{\\n\\t// ...\\n\\t\\n\\tprivate int _years;\\n\\t\\n\\tpublic CustomerBuilder HasBeenMemberForYears(int years)\\n\\t{\\n\\t\\t_years = years;\\n\\t\\treturn this;\\n\\t}\\n\\t\\n\\tprotected override void AlterProxy(Customer proxy)\\n\\t{\\n\\t\\tproxy.CustomerForHowManyYears(Arg.Any<DateTime>()).Returns(_years);\\n\\t}\\n\\t\\n\\t// ...\\n}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nThen in your test you can use:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var customer = new CustomerBuilder()\\n\\t.AsProxy()\\n\\t.HasBeenMemberForYears(10);\\nvar years = customer.CustomerForHowManyYears(DateTime.Now); // 10\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\n*Remember that when using proxy objects of real classes that you need to mark properties and methods as virtual and have a protected empty constructor.*","excerpt":"","slug":"creating-proxy-objects","type":"basic","title":"Creating proxy objects"}

Creating proxy objects


This library integrates with [NSubstitute](http://nsubstitute.github.io/) for generating proxy objects, this means you can call the `AsProxy` method on your builder to request that the result from calling `Build` will be an NSubstitute proxy with the public properties set to return the values you have specified via your builder, e.g. [block:code] { "codes": [ { "code": "var customer = CustomerBuilder.WithFirstName(\"Rob\").AsProxy().Build();\ncustomer.CustomerForHowManyYears(Arg.Any<DateTime>()).Returns(10);\nvar name = customer.FirstName; // \"Rob\"\nvar years = customer.CustomerForHowManyYears(DateTime.Now); // 10", "language": "csharp" } ] } [/block] If you need to alter the proxy before calling `Build` to add complex behaviours that can't be expressed by the default public properties returns values then you can override the `AlterProxy` method in your builder, e.g. [block:code] { "codes": [ { "code": "class CustomerBuilder : TestDataBuilder<Customer, CustomerBuilder>\n{\n\t// ...\n\t\n\tprivate int _years;\n\t\n\tpublic CustomerBuilder HasBeenMemberForYears(int years)\n\t{\n\t\t_years = years;\n\t\treturn this;\n\t}\n\t\n\tprotected override void AlterProxy(Customer proxy)\n\t{\n\t\tproxy.CustomerForHowManyYears(Arg.Any<DateTime>()).Returns(_years);\n\t}\n\t\n\t// ...\n}", "language": "csharp" } ] } [/block] Then in your test you can use: [block:code] { "codes": [ { "code": "var customer = new CustomerBuilder()\n\t.AsProxy()\n\t.HasBeenMemberForYears(10);\nvar years = customer.CustomerForHowManyYears(DateTime.Now); // 10", "language": "csharp" } ] } [/block] *Remember that when using proxy objects of real classes that you need to mark properties and methods as virtual and have a protected empty constructor.*