Live data from Hacker News

Learn how to test Rails using Test::Unit instead of Rspec

whatdoitest.com

31–40 of 51 posts

Re: Learn how to test Rails using Test::Unit instead of Rspec

#32
post #2

I was a little disappointed in this article. What I've always wanted to know is: Why do Rails developers use Rspec when they can use MiniTest::Spec? I was hoping the OP would address that, since MiniTest is the default Ruby testing suite he refers to. Do Rails devs continue to use Rspec because it's what they're used to? Or is Rspec a dependency for other testing tools and libraries? I don't agree at all with this pr…

My biggest problem was the matchers and mock support in MiniTest::Spec. I think RSpec is further ahead here with better support for both. Avdi Grimm agrees: https://twitter.com/avdi/status/180394364357443584

Re: Learn how to test Rails using Test::Unit instead of Rspec

#33

God yes. Test::Unit is simple, comes baked in, and because it's not some faux-English DSL your brain isn't constantly fighting with itself making stupid and incorrect assumptions about syntax because 'hey, it looks like English'. The worst testing-related decision I ever made was using Michael Hardt's Rails Tutorial (which has RSpec baked in) to learn Rails in the first place. Great tutorial otherwise, but it set my…

> it's not some faux-English DSL your brain isn't constantly fighting with itself making stupid and incorrect assumptions about syntax because 'hey, it looks like English'.

I'm doing the same Rails tutorial right now and I constantly feel like this when it comes to writing the RSpec stuff. It just doesn't feel intuitive and logical, in spite of (or maybe because of) its efforts of being just that. The tutorial doesn't really dive into the specifics of it, it's just "Monkey write, Monkey do" without being able to grasp the concepts of it. This completely leaves me in the dark when it comes to writing my own tests.

Re: Learn how to test Rails using Test::Unit instead of Rspec

#34

I hate to be that guy , but the Rspec tests should be written more like the following: describe 'Article' do let(:article) { Article.new } describe '#slug' do before(:each) do article.title = "Testing with Test::Unit" end it 'does not contain non-alphabetical characters' do article.slug.should_not match(/[^\w-]/) end it 'does not contain capital letters' do article.slug.should_not match(/[A-Z]/) end it 'replaces spac…

one step further: describe Article do describe '#slug' do before do subject.title = "Testing with Test::Unit" end its(:slug) { should_not match(/[^\w-]/) } its(:slug) { should_not match(/[A-Z]/) } it 'replaces spaces with hyphens' end end

I like and use RSpec, but I think these improvements are proving the guy's point that its DSL is forbidding for beginners. I write RSpec more like the original post because it's less magic.

Re: Learn how to test Rails using Test::Unit instead of Rspec

#35

I hate to be that guy , but the Rspec tests should be written more like the following: describe 'Article' do let(:article) { Article.new } describe '#slug' do before(:each) do article.title = "Testing with Test::Unit" end it 'does not contain non-alphabetical characters' do article.slug.should_not match(/[^\w-]/) end it 'does not contain capital letters' do article.slug.should_not match(/[A-Z]/) end it 'replaces spac…

one step further: describe Article do describe '#slug' do before do subject.title = "Testing with Test::Unit" end its(:slug) { should_not match(/[^\w-]/) } its(:slug) { should_not match(/[A-Z]/) } it 'replaces spaces with hyphens' end end

Not really. At least RSpec's creator doesn't think so (http://blog.davidchelimsky.net/2012/05/13/spec-smell-explici...).

Re: Learn how to test Rails using Test::Unit instead of Rspec

#36

God yes. Test::Unit is simple, comes baked in, and because it's not some faux-English DSL your brain isn't constantly fighting with itself making stupid and incorrect assumptions about syntax because 'hey, it looks like English'. The worst testing-related decision I ever made was using Michael Hardt's Rails Tutorial (which has RSpec baked in) to learn Rails in the first place. Great tutorial otherwise, but it set my…

I write tests primarily with Rspec for personal projects and Cucumber at work. I haven't worked with unit test at all. I like both Rspec and Cucumber. I use Rspec for personal projects because they are more backend Rails side heavy web app. And my company uses Cucumber because we found writing tests based on user behavior to make more sense.

My problem with Rspec is in its error message, whether it is showing why the test failed or that the test code syntax is wrong. I noticed that Rspec is more prone to showing cryptic errors than Cucumber. I've seen some mind boggling error messages that doesn't really help user debug the code. For devs beginning TDD, I can understand why working with Rspec could be annoying. Even now, Rspec doesn't feel second nature to me.

Re: Learn how to test Rails using Test::Unit instead of Rspec

#37

I hate to be that guy , but the Rspec tests should be written more like the following: describe 'Article' do let(:article) { Article.new } describe '#slug' do before(:each) do article.title = "Testing with Test::Unit" end it 'does not contain non-alphabetical characters' do article.slug.should_not match(/[^\w-]/) end it 'does not contain capital letters' do article.slug.should_not match(/[A-Z]/) end it 'replaces spac…

Even better with the new syntax:

    expect(article.slug).to match(/[^\w-]/)

Re: Learn how to test Rails using Test::Unit instead of Rspec

#39
post #8

My experience: I just started the process of converting our few hundred Test::Unit tests to Rspec simply because Test Unit documentation is terrible . Our tests need a lot of work and I'm not that experienced at writing test scripts. I have to learn this as I do it, and I struggled to find resources for learning Test::Unit. Every blog post, tutorial and guide I can find was written for Rspec. As much as I'd love to c…

This was my experience too. I tried to use Test::Unit because I was learning from scratch, Rails 2.something, and Test::Unit was "The Rails Way". But I was fumbling in the dark. The community around RSpec picked me up and carried me to the testing promised land.

This. That was totally my experience too, after freaking out learning rails the friendly rspec syntax and natural readability felt like I'd reached a pub while lost in the desert.

Re: Learn how to test Rails using Test::Unit instead of Rspec

#40
post #2

I was a little disappointed in this article. What I've always wanted to know is: Why do Rails developers use Rspec when they can use MiniTest::Spec? I was hoping the OP would address that, since MiniTest is the default Ruby testing suite he refers to. Do Rails devs continue to use Rspec because it's what they're used to? Or is Rspec a dependency for other testing tools and libraries? I don't agree at all with this pr…

I've been using Minitest, Mocha, and RR lately after using Rspec for many years, and find myself really missing Rspec.

At a high level, I would say that Rspec is just much more thought out. The other libraries all seem to be written by someone who looked at Rspec and though "Ugh, this is too much. I just need X."

But I find, if I'm doing serious TDD, I'm gonna need pretty all of Rspec. And what happens is that the other libs just don't integrate that well.

For example, you may be using one test runner (minitest) with a mock from some other library (mocha) and assertions from a third (test::unit). The question is: are those things all meant to work well together? Are your mocks scoped correctly within your test runner? Is your database cleaner working properly with your before and after hooks?

The answer is sometimes "no". But with Rspec, everything works well together. It just fits. Generally I'm a big fan of focused libraries, loosely coupled. But the state of things with testing is that I don't think we've yet found that good loose coupling.

The second issue is that Rspec just seems to have much richer assertions. I can do things like:

    expect {
      delete_zombie_users
    }.to change(User, :count).by(-1)
Or:

    thingy.length.should 
And mocking is also underdeveloped in other libs. Mocha, for example, can't stub constants. In Rspec I can do:

    User.stub(:all => [fred, betty, sue])
Or

    stub_const("File::MAX_SIZE", 10**10)
And it works great. I find I can easily stub out parts of ActiveRecord and write good targetted unit tests. With Mocha I find myself using any_instance all the time because I can't target things on the structure I really want to target them to.

Some of these are available in other expectation libraries, but I've found Rspec to have a really nice, comprehensive set of them.

All in all, I just feel like David Chelimsky and the Rspec folks have really thought about testing in a way the other libs' authors haven't. I keep learning things about TDD and finding out that Rspec has already considered them. With other libs the opposite often seems true.

Post reply on HN