{"_id":"56ac21d2c69a430d008aabb3","version":{"_id":"54471fc9e12a270800028adf","__v":10,"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"},"category":{"_id":"5447ed230319802200fc0702","__v":7,"version":"54471fc9e12a270800028adf","pages":["5447ef8d0319802200fc0713","56ac1ec3aa91300d00bede9b","56ac213d0bbe170d00f11272","56ac21d2c69a430d008aabb3","56b1401d4b05c50d001ceb3b","56c72ca5862f940d0056475d","56c75ec053cafe0d00a53d7b"],"project":"54471fc9e12a270800028adc","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2014-10-22T17:45:07.793Z","from_sync":false,"order":6,"slug":"convention-system","title":"Convention System"},"user":"54471f91beb6320800da6f75","__v":7,"project":"54471fc9e12a270800028adc","updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-01-30T02:37:06.278Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":3,"body":"Behaviors all derive from the base SpecsFor.Behavior<TType> class.  For a behavior that will apply to all specs, TType will be ISpecs, the non-generic interface that all specs implement.  Here's our LogExecutionTimeBehavior:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"using SpecsFor;\\nusing SpecsFor.Configuration;\\n\\nnamespace Conventions.Basics\\n{\\n    public class LogExecutionTimeBehavior : Behavior<ISpecs>\\n    {\\n    }\\n}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nYou can override virtual methods from the base Behavior class in order to define logic that should be executed during a spec's execution.  The available methods are:\n\n* SpecInit - Executed once SpecsFor's [auto-mocking container](doc:about-auto-mocking) has initialized.\n* ClassUnderTestInitialized - Executed once the [System Under Test](doc:the-system-under-test-sut) has been instantiated.\n* Given - Executed **before** the spec's Given method is executed.  This is a great place to apply state across specs. \n* AfterSpec - Executed **after** the spec's AfterSpecs method has been executed.  You can hook in here to do cleanup, if necessary.\n\nFor our behavior, we'll want to capture the time between when the spec is initialized (SpecInit) and when it ends (AfterSpec).  We can do that using the Stopwatch class: \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"    public class LogExecutionTimeBehavior : Behavior<ISpecs>\\n    {\\n        private Stopwatch _stopwatch;\\n\\n        public override void SpecInit(ISpecs instance)\\n        {\\n            _stopwatch = Stopwatch.StartNew();\\n        }\\n\\n        public override void AfterSpec(ISpecs instance)\\n        {\\n            _stopwatch.Stop();\\n\\n            Console.WriteLine($\\\"{instance.GetType().Name} - {_stopwatch.Elapsed}\\\");\\n        }\\n    }\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nNow when we run our specs, our behavior will output the execution time for each one. \n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/lKxXIIpQY6YJvvLmgbkA_tempscreenshot.PNG\",\n        \"tempscreenshot.PNG\",\n        \"1167\",\n        \"167\",\n        \"#61afd0\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\nAdmittedly, this isn't the most-useful behavior.  Let's look at something a little more useful.\n\n[Continue on to defining state with a behavior](doc:using-a-behavior-to-define-state)","excerpt":"","slug":"creating-a-behavior","type":"basic","title":"Creating a behavior"}

Creating a behavior


Behaviors all derive from the base SpecsFor.Behavior<TType> class. For a behavior that will apply to all specs, TType will be ISpecs, the non-generic interface that all specs implement. Here's our LogExecutionTimeBehavior: [block:code] { "codes": [ { "code": "using SpecsFor;\nusing SpecsFor.Configuration;\n\nnamespace Conventions.Basics\n{\n public class LogExecutionTimeBehavior : Behavior<ISpecs>\n {\n }\n}", "language": "csharp" } ] } [/block] You can override virtual methods from the base Behavior class in order to define logic that should be executed during a spec's execution. The available methods are: * SpecInit - Executed once SpecsFor's [auto-mocking container](doc:about-auto-mocking) has initialized. * ClassUnderTestInitialized - Executed once the [System Under Test](doc:the-system-under-test-sut) has been instantiated. * Given - Executed **before** the spec's Given method is executed. This is a great place to apply state across specs. * AfterSpec - Executed **after** the spec's AfterSpecs method has been executed. You can hook in here to do cleanup, if necessary. For our behavior, we'll want to capture the time between when the spec is initialized (SpecInit) and when it ends (AfterSpec). We can do that using the Stopwatch class: [block:code] { "codes": [ { "code": " public class LogExecutionTimeBehavior : Behavior<ISpecs>\n {\n private Stopwatch _stopwatch;\n\n public override void SpecInit(ISpecs instance)\n {\n _stopwatch = Stopwatch.StartNew();\n }\n\n public override void AfterSpec(ISpecs instance)\n {\n _stopwatch.Stop();\n\n Console.WriteLine($\"{instance.GetType().Name} - {_stopwatch.Elapsed}\");\n }\n }", "language": "csharp" } ] } [/block] Now when we run our specs, our behavior will output the execution time for each one. [block:image] { "images": [ { "image": [ "https://files.readme.io/lKxXIIpQY6YJvvLmgbkA_tempscreenshot.PNG", "tempscreenshot.PNG", "1167", "167", "#61afd0", "" ] } ] } [/block] Admittedly, this isn't the most-useful behavior. Let's look at something a little more useful. [Continue on to defining state with a behavior](doc:using-a-behavior-to-define-state)