{"action":"created","issue":{"url":"https://api.github.com/repos/pester/Pester/issues/1188","repository_url":"https://api.github.com/repos/pester/Pester","labels_url":"https://api.github.com/repos/pester/Pester/issues/1188/labels{/name}","comments_url":"https://api.github.com/repos/pester/Pester/issues/1188/comments","events_url":"https://api.github.com/repos/pester/Pester/issues/1188/events","html_url":"https://github.com/pester/Pester/issues/1188","id":394772559,"node_id":"MDU6SXNzdWUzOTQ3NzI1NTk=","number":1188,"title":"Can I use mock to overwrite Context, in order to implement a BeforeEach for every context?","user":{"login":"Caoxuyang","id":15007516,"node_id":"MDQ6VXNlcjE1MDA3NTE2","avatar_url":"https://avatars2.githubusercontent.com/u/15007516?v=4","gravatar_id":"","url":"https://api.github.com/users/Caoxuyang","html_url":"https://github.com/Caoxuyang","followers_url":"https://api.github.com/users/Caoxuyang/followers","following_url":"https://api.github.com/users/Caoxuyang/following{/other_user}","gists_url":"https://api.github.com/users/Caoxuyang/gists{/gist_id}","starred_url":"https://api.github.com/users/Caoxuyang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Caoxuyang/subscriptions","organizations_url":"https://api.github.com/users/Caoxuyang/orgs","repos_url":"https://api.github.com/users/Caoxuyang/repos","events_url":"https://api.github.com/users/Caoxuyang/events{/privacy}","received_events_url":"https://api.github.com/users/Caoxuyang/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-12-29T08:04:49Z","updated_at":"2019-01-04T08:54:54Z","closed_at":null,"author_association":"NONE","body":"Hi I would like a BeforeEach for every context in my test script.\r\nSo I wonder Can I do something like: \r\nMock Context{\r\n //preserving all the context codes\r\n\r\n //then add my additional codes\r\n}\r\n\r\nCan I do this in this way?"},"comment":{"url":"https://api.github.com/repos/pester/Pester/issues/comments/451387329","html_url":"https://github.com/pester/Pester/issues/1188#issuecomment-451387329","issue_url":"https://api.github.com/repos/pester/Pester/issues/1188","id":451387329,"node_id":"MDEyOklzc3VlQ29tbWVudDQ1MTM4NzMyOQ==","user":{"login":"nohwnd","id":5735905,"node_id":"MDQ6VXNlcjU3MzU5MDU=","avatar_url":"https://avatars2.githubusercontent.com/u/5735905?v=4","gravatar_id":"","url":"https://api.github.com/users/nohwnd","html_url":"https://github.com/nohwnd","followers_url":"https://api.github.com/users/nohwnd/followers","following_url":"https://api.github.com/users/nohwnd/following{/other_user}","gists_url":"https://api.github.com/users/nohwnd/gists{/gist_id}","starred_url":"https://api.github.com/users/nohwnd/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nohwnd/subscriptions","organizations_url":"https://api.github.com/users/nohwnd/orgs","repos_url":"https://api.github.com/users/nohwnd/repos","events_url":"https://api.github.com/users/nohwnd/events{/privacy}","received_events_url":"https://api.github.com/users/nohwnd/received_events","type":"User","site_admin":false},"created_at":"2019-01-04T08:54:54Z","updated_at":"2019-01-04T08:54:54Z","author_association":"MEMBER","body":"You can do that, but it would be easier just to wrap the Context/Describe block in a function and implement the setup and teardown there: \r\n\r\n```powershell\r\nfunction ContextWithSetupAndTeardown (\r\n    $Name, \r\n    $Body, \r\n    $Setup = {}, \r\n    $Teardown = {}\r\n) {\r\n    try {\r\n        & $Setup\r\n        Context $Name $Body\r\n    }\r\n    finally {\r\n        & $Teardown\r\n    }\r\n}\r\n\r\nDescribe \"a\" {\r\n    ContextWithSetupAndTeardown -Name \"b\" `\r\n        -Setup { Write-Host \"Setup <--\" -ForegroundColor Cyan } `\r\n        -Body { \r\n            It \"passes\" { \r\n                $true | Should -Be $true\r\n            }\r\n        } `\r\n        -Teardown { Write-Host \"Teardown <--\" -ForegroundColor Cyan }\r\n}\r\n```\r\n```\r\nDescribing a\r\nSetup <--\r\n\r\n  Context b\r\n    [+] passes 82ms\r\nTeardown <--\r\n```"}}
{"actor":{"display_login":"nohwnd"}}