Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignoring hook context #242

Closed
nh2 opened this issue Aug 12, 2015 · 5 comments
Closed

Ignoring hook context #242

nh2 opened this issue Aug 12, 2015 · 5 comments

Comments

@nh2
Copy link
Member

nh2 commented Aug 12, 2015

Hi, I found myself to use the following (see full example:

-- | When a hook is set up with `beforeAll`, that changes the type
-- of all hspec `it`s to take the value supplied by `beforeAll`.
--
-- In some cases, we just want to use `beforeAll`/`afterAll` to have
-- a process (e.g. a web server) running during the tests, and the
-- individual `it`s may not care about the value supplied by `beforeAll`.
--
-- In such cases, this function can be used to allow to use plain `it`s
-- within a `describe` block that would usually mandate all `it`s to
-- accept the `a` that `beforeAll` provided.
ignoreHookContext :: SpecWith () -> SpecWith a
ignoreHookContext = Hooks.aroundWith (\actionRunner -> const (actionRunner ()))


spec :: Spec -- == SpecWith ()
spec =
    beforeAll (putStrLn "beforeAll hook" >> threadDelay 2000000 >> return (3 :: Int))
  . afterAll (\i -> putStrLn ("afterAll hook: " ++ show (i::Int)))
  $ do

  describe "section using the hook return value" $ do
    it "does X" $ \i -> do
      putStrLn $ "done, with access to value " ++ show i

  ignoreHookContext $ do
    describe "section NOT using the hook return value" $ do
      it "does Y" $
        putStrLn "done"

Is this a sensible pattern to use?

Should it be a helper function in hspec?

@soenkehahn
Copy link
Contributor

This seems like a sensible pattern, yes. Maybe it would even make sense to have this as a primitive and implement before_ and friends in terms of it.

However both @sol and me are still experimenting with approaches for allowing multiple arguments to spec items. We tend to defer this issue until we have a proper solution. (We're afraid of additional breakage.)

If this is important to you, we would be happy to include it now though. Personally I use it "foo" $ \ _ -> ....

@sol sol added this to the 2.6.0 milestone Mar 14, 2018
@sol
Copy link
Member

sol commented Jun 28, 2018

I think this is a specialization #353.

@sol
Copy link
Member

sol commented Jun 28, 2018

However both @sol and me are still experimenting with approaches for allowing multiple arguments to spec items.

I'm tempted to ditch this. Having only one argument to spec items actually nicely fits the concept of "subject under test" (see my prototype for RSpec-style one-liner it-blocks, https://github.com/hspec/hspec-subject).

@sol
Copy link
Member

sol commented Jun 29, 2018

With #360 ignoreHookContext becomes

ignoreHookContext :: SpecWith () -> SpecWith a
ignoreHookContext = mapSubject (const ())

Assuming we add mapSubject, do we still want a separate primitive for this?

@sol sol removed this from the 2.6.0 milestone May 8, 2019
@sol sol added this to the later milestone Apr 18, 2021
@sol sol removed this from the later milestone May 12, 2021
@sol sol closed this as completed in 0fc5cb1 May 14, 2021
@sol
Copy link
Member

sol commented May 14, 2021

mapSubject / ignoreSubject is on Hackage with hspec-2.8.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants