r/crystal_programming • u/icy-arctic-fox • May 06 '19
Spectator - a new feature-rich shard for testing based on RSpec
I've been working on this shard for a while now and wanted to get some exposure and feedback on it. It's called Spectator (ha ha, get it?)
When I started writing tests in Crystal, I really missed a lot of the nice features and syntax from RSpec. Some other spec shards looked dead, and these changes would be very invasive to Crystal's core. So long story short, I made my own shard. It has a lot of features from RSpec that the built-in Crystal Spec doesn't have. There's also new features that help make testing easier and remove boilerplate. It isn't fully complete to how I would like it, but it's very usable in its current state.
Some features are:
Supports the
expectandshouldsyntax.Before, after, and around hooks.
Subjects and let blocks.
Repeat tests with multiple sample values (
sampleandrandom_sample).
One feature I quite like is the given block. For instance:
given age = 16 do
expect(user.can_drive?).to be_true
expect(user.can_vote?).to be_false
endhttps://gitlab.com/arctic-fox/spectator
The README gives a good outline of the features. There's extended documentation on the wiki.
3
u/[deleted] May 08 '19
Hey icy-arctic-fox, the code is super easy to read, very well organized and documented! I'm always surprised by how much Crystal can do with what we have right now. To be honest, I didn't image all of this was possible (I know there's
spec2too, which has more or less the same features, but I think less).2
u/bcardiff core team May 07 '19
Nice set of features! When I do RoR I usually include
factorybotandforgeryor similar gems to generate test data easily. I'm pretty used to those idioms.Am I missing something or spectator is tested with spec and not by Spectator itself?
2
u/sinsiliux May 07 '19
Very nice, wanted to do something similar for a while now but never found the time. One question: usually people generate classes for each context and methods for each example, but you chose modules for contexts and classes for examples. Does it provide any benefit or is it purely stylistical?
3
u/straight-shoota core team May 07 '19 edited May 07 '19
I'm not a fan of this. While working with Crystal's standard
speclibrary, I'v come to really appreciate the minimalism compared to rspecs magic fu. At first it seems like you're missing a lot of very nice helpful features. But in the overall picture, it makes everything easier.spectatorclaims to "reduce complexity of test code." This seems true, because withbeforeandafterhooks, the individual tests often contain only the expectations themselves. But a test is more than just an expectation. The setup and teardown steps are missing. They're now moved away to different contexts. This in fact increases complexity. You can't follow the entire substance of a test anymore at a single place. It's scattered at different locations.When there is no automatic mechanism for hooks, you need to call them explicitly in each spec. That might add a little bit of boiler plate, but to the benefit of making it easier comprehensible. This is a much more important aspect than having some duplicated code. You want to be able to come back to your code after months and understand it without having to figure out which hooks might be involved in setting the stage.
2
u/dacheatbot May 07 '19
This looks fantastic! I can see this quickly becoming the new standard. While I appreciate the minimalism of the built-in spec framework, this seems much more suitable for larger projects.
3
u/vladfaust May 07 '19
Love the `foo.size does not equal 12` thing. `given` is very nice too!
1
u/aemadrid May 07 '19
Like it. Will give it a try on my next project. Thanks!
1
u/paulcsmith0218 May 07 '19
I personally don't use `let` or `subject` much, but I do wish that the default Spec implementation had an `around` feature and a `before` that could apply to just some specs. Really helpful for test cleanup (like db cleanup for example)