3 ms·
I always found it a bit useless to be fussing over phrasing like this. What does it matter if we call it expect(x).to(equal(y)) or x.should_be(y) or assert
by colonwqbang 5y ago
I always found it a bit useless to be fussing over phrasing like this. What does it matter if we call it
expect(x).to(equal(y))
or
x.should_be(y)
or
assert_eq(x, y)
These are just different ways of saying the same thing. The first two aren't really better than the last at conveying what's going on.
Although I like Ruby and Rspec, this desire for "beautiful code" and "making the code read like English" never rhymed with me.
- masklinn 5y ago> The first two aren't really better than the last at conveying what's going on. Though I've never really liked the verbiage, TFA does correctly remark that it guides towards the correct parameter ordering in ways `assert_eq` does not. And depending on the way assertion errors are formatted, getting the ordering wrong can lead to odd / awkward messages. 'course these days I use pytest which just asks for an `assert` and takes care of taking apart the expression being asserted, so it's a non-issue.
- tmp538394722 5y agoThe first 1/3 of the article explains their historical motivation for this approach and specifically why this was a breakthrough for teaching. Names can matter. And from a more practical standpoint, RSpec is now much more than just a different mapping of some key words. the DSL rspec landed on can be very concise, which I appreciate. This is mostly made possible by Ruby’s short block syntax and the ability to nest them. One thing this enables is an easy way to define a hierarchy of concerns with their own nested setup/tear down blocks, which allows you to easily share code across similar example groups. This is nice in and of itself, but it also leads you to differentiate the “boring setup” code from the “here’s the part I’m actually testing code” which is helpful for clarity when coming back to old tests. These things are sharp knives and can be used poorly, but I’ve never yet found a system I like better.
- srbaker 5y agoExactly this. My main point, I think, is that once people learned how to _think_ about these problems, they could be shown the "normal" syntax and it would click. This is why I thought it was valuable as a teaching tool, and not valuable beyond that. Others disagreed. I'm just a dude trying to help people, though. What do I know?
- notsureaboutpg 5y ago"making the code read like English" is what doomed Semantic UI. There was a brief period of popularity for it, but looking at all the popular Bootstrap-type libraries now, none of them try to be "semantic" or "read like English" Because at the end of the day the function of the code is not the function of speaking or writing the English language. Styling front-end components does not work like the English language works. I can call something "big" in English and everyone knows what I mean, but the browser will determine what an element having the "big" class means based on the context of its parent elements and its children elements. That difference makes it extra confusing to use code that's trying to be "semantic" especially if the "semantics" are trying to hide more expressive and complicated functionality from the user.
- srbaker 5y agoIt's true. This is why I never understood why people used RSpec for real software. It was only intended to teach people how to think.
- kofejnik 5y agoyeah, could never understand what's wrong with assert(x == y). Why learn yet another awkward DSL on top of all other DSLs and hidden conventions, which RoR already has a lot (which kind of turned me away from it)
- hombre_fatal 5y agoI’m surprised the approach of https://github.com/sconover/wrong https://github.com/sconover/wrong never caught on where you just write assert { x == y } and it inspects the values you’re comparing and the methods you’re using to generate the appropriate error messages and, say, a diff of where two arrays diverge.
- nerdponx 5y agoThis is exactly what the Pytest framework in Python does. It's very, very convenient.
- srbaker 5y agoI am a fan of pytest, and mightn't have thought to make the same thing in Python land.
- aYsY4dDQ2NrcNzA 5y agoAnd in fact, pytest gives you hooks for these various kinds of value comparisons so that you can generate any error message that you want.
- srbaker 5y agoA generic assert that fails on AssertionFailedException, and errors on all other exceptions is the ideal.