{"__v":1,"_id":"56c72ca5862f940d0056475d","category":{"__v":7,"_id":"5447ed230319802200fc0702","pages":["5447ef8d0319802200fc0713","56ac1ec3aa91300d00bede9b","56ac213d0bbe170d00f11272","56ac21d2c69a430d008aabb3","56b1401d4b05c50d001ceb3b","56c72ca5862f940d0056475d","56c75ec053cafe0d00a53d7b"],"project":"54471fc9e12a270800028adc","version":"54471fc9e12a270800028adf","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2014-10-22T17:45:07.793Z","from_sync":false,"order":6,"slug":"convention-system","title":"Convention System"},"project":"54471fc9e12a270800028adc","user":"54471f91beb6320800da6f75","version":{"__v":10,"_id":"54471fc9e12a270800028adf","project":"54471fc9e12a270800028adc","createdAt":"2014-10-22T03:08:57.750Z","releaseDate":"2014-10-22T03:08:57.750Z","categories":["54471fc9e12a270800028ae0","5447b9e7b96a63140077d747","5447be130319802200fc0620","5447ed118d7af31a00dd411c","5447ed230319802200fc0702","5448524c4544c30800241f41","544854504544c30800241f4d","544854af4544c30800241f50","544854e74544c30800241f51","54485557c1b42e08005b82bf"],"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":"2016-02-19T14:54:29.581Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":999,"body":"One very common use for a behavior is to wrap a spec in a transaction scope.  This way, any changes the spec makes can be rolled back once the spec executes.\n\nFirst, let's register the behavior:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"WhenTesting<INeedATransaction>().EnrichWith<TransactionScopeWrapperBehavior>();\\n\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nNow we just need the behavior and the marker interface:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"    public interface INeedATransaction : ISpecs\\n    {\\n    }\\n\\n    public class TransactionScopeWrapperBehavior : Behavior<INeedATransaction>\\n    {\\n        private TransactionScope _scope;\\n\\n        public override void SpecInit(INeedATransaction instance)\\n        {\\n            _scope = new TransactionScope();\\n        }\\n\\n        public override void AfterSpec(INeedATransaction instance)\\n        {\\n            _scope.Dispose();\\n        }\\n    }\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nAny spec can now automatically wrap itself in a transaction scope just by implementing the INeedATransaction interface.","excerpt":"","slug":"wrapping-a-spec-with-a-behavior","type":"basic","title":"Wrapping a spec with a behavior"}

Wrapping a spec with a behavior


One very common use for a behavior is to wrap a spec in a transaction scope. This way, any changes the spec makes can be rolled back once the spec executes. First, let's register the behavior: [block:code] { "codes": [ { "code": "WhenTesting<INeedATransaction>().EnrichWith<TransactionScopeWrapperBehavior>();\n", "language": "csharp" } ] } [/block] Now we just need the behavior and the marker interface: [block:code] { "codes": [ { "code": " public interface INeedATransaction : ISpecs\n {\n }\n\n public class TransactionScopeWrapperBehavior : Behavior<INeedATransaction>\n {\n private TransactionScope _scope;\n\n public override void SpecInit(INeedATransaction instance)\n {\n _scope = new TransactionScope();\n }\n\n public override void AfterSpec(INeedATransaction instance)\n {\n _scope.Dispose();\n }\n }", "language": "csharp" } ] } [/block] Any spec can now automatically wrap itself in a transaction scope just by implementing the INeedATransaction interface.