Live data from Hacker News

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

whatdoitest.com

41–50 of 51 posts

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

#41
Some jarring issues with testing in the Rails community, and these aren't things I recognized until about 3 years into it all (in other words, recently) and finally had the wherewithal and exposure to other tools/paradigms where I could finally criticize my tools.

* Testing is an advanced topic, yet it seems to be misleadingly sold in Rails tutorial literature as something that everyone else effortlessly does with the same ease of "gem install hairball".[3]

* Good luck finding resources on Test::Unit/Minitest.[1]

* Where the documentation?

* Rails invents its own definitions for "unit tests", "functional tests", and "integration tests". Even though they already exist in testing vernacular. [2] Unfortunately confusing when Rails is your first experience with testing and you think "unit test" means "model test" and "integration test" means fumbling fruitlessly with your jasmine/capybara frankensuite because you're lost like I was and tried to stitch together a few Railscasts because you just don't know any better.

* Instead of embracing tests that don't depend on the Rails stack, the Rails community embraces tools like Spork that keep a Rails process alive. Because Rails takes too long to boot to have a test-driven cycle with tests that depend on Rails. The concept of service objects and wrapping Rails/3rd party libs is brand new to me and I only encountered it after I was good enough to contribute to large Rails apps and read their source code.

[1]: At least Ruby 1.9 changed the name (in essence) to "minitest" which we can finally google. "test unit"? Not so easy to google.

Also, here's the best resource for Minitest I can find: http://www.mattsears.com/articles/2011/12/10/minitest-quick-.... It's a cheatsheet on someone's blog.

Frankly, there are much better arguments for sticking with Rspec. For one, it's the testing library that the community uses and you need community support. The popular gems you'll be using probably have more wiki info on Rspec testing too (like Devise). Rspec is documented and even illustrates best practices. http://betterspecs.org/.

That Minitest is built in to Ruby was never a convincing reason to use it. I already have development dependencies. Trust me that testing isn't going to be the thing that introduces the first 3rd party dependency into my app. Fucking "awesome_print" is already in my Gemfile, so it's not a big deal to add a gem that makes testing better for me.

And for all the hate that Rspec gets for its DSL, Test::Unit/Minitest's DSL (assertion statements) is just as bad and Minitest::Spec introduces an Rspec DSL anyways.

The only way I want to write tests is by writing my assertions in pure Ruby without having to lug around a bunch of different assertion variants just so the testing library knows what kind of error output to show me.

The ONLY solution I've found is a library called Wrong (https://github.com/sconover/wrong) and it works with both Minitest and Rspec.

It lets you write assertions like this:

    include Wrong
    assert { "abc" == "abd" } 
    assert { cookie_jar.empty? }
    assert { dog.respond_to?(:walk) }
    assert { boy.is_a? Human }
Like, actual Ruby. And it will figure out on its own how to display diffs in error output.

Meanwhile, here's Minitest:

    assert_equal "abc", "abd"
    assert_empty cookie_jar
    assert_responds_to dog, :walk
    assert_instance_of Human, boy
    # I know these because I'm looking at a cheatsheet
Here's Rspec:

    "abc".should == "abd"
    cookie_jar.should be_empty
    it ->(dog){responds_2}.(:walk).(&:does?)
So all this talk about avoiding DSLs yet you don't. And it wasn't until blowmage (of minitest) told me about Wrong in IRC that I found what I wanted -- the only real way to avoid DSLs and verbosity in Ruby testing that I've come across.

[2]: Xavier Shay brings this up in his talk http://www.confreaks.com/videos/815-larubyconf2012-rails-sus...

[3]: Oh yeah, and after three years of Ruby and Rails, it wasn't until I was exposed to functional programming (Martin Odersky, Rich Hickey), OO design (Sandi Metz), and testing screencasts (Gary Bernhardt) that I finally understood how to even fundamentally write code that could be tested. In other words, it took high level a-ha moments for me to be able to write tests that didn't just cripple my workflow and waste my time and slow down my learning process. That's where I found out that testing is an advanced topic, not a bullet point you can throw into a newbie tutorial on how to generate a Rails scaffold.

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

#42
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…

"because Test Unit documentation is terrible"

Are you confusing Test Unit documentation with 'How/What to test' documentation? I can't think of any documentation required if what you use is a group of asserts.

Test::Unit in a nutshell: `test "should do something"; ...; end` is what gets run in each of the files. `setup` is called before each test. `teardown` is called after each test. use `assert_*` at your discretion.

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

#43
post #28
post #10

Earlier quoted context omitted.

Hear hear. I am dreading the technical debt I have in test-unit, and meanwhile the (Rails) world is moving on to Minitest and the world appears to be settling on Rspec syntax regardless. Nowhere to be found is even a conversion HOWTO, so test-unit starts feeling even more like a ghetto, and increases my conversion anxiety, which is all too bad, since I like what I consider to be the simplicity of test-unit.

>I am dreading the technical debt I have in test-unit, and meanwhile the (Rails) world is moving on to Minitest Minitest and Test::Unit are part of the same package since 1.9, with Test::Unit implemented as a simple compatibility layer on top of Minitest: http://www.rubyinside.com/a-minitestspec-tutorial-elegant-sp... The Minitest assertions are quite similar to the old Test::Unit assertions, a large number are ident…

Right, and I've seen many pages like the rubyinside one leaving me still hungry with questions like, "So, no more test_helper.rb or what?" Couldn't find that in either of those links.

This is really more of a general documentation quibble on my part though, since I'm the dope who can't read through my questions and find the answers in the source (or wherever). I've been planning on making a blog post about 1:1 mapping of concepts between Test::Unit and Minitest learned from my own transformation, but as I've mentioned, I haven't been able to undertake that yet.

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

#44
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…

"because Test Unit documentation is terrible" Are you confusing Test Unit documentation with 'How/What to test' documentation? I can't think of any documentation required if what you use is a group of asserts. Test::Unit in a nutshell: `test "should do something"; ...; end` is what gets run in each of the files. `setup` is called before each test. `teardown` is called after each test. use `assert_*` at your discretio…

You know, recently I had reason to look into the teardown method (DatabaseCleaner, dontchaknow), and I never did find anything conclusive that it was something that test-unit used. Sure, it was mentioned in many many DatabaseCleaner questions/answers/threads, but nothing I could really rely on since invariably these were also using Rspec. That is, my unanswered question was "Is teardown solely an Rspec thing?"

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

#46
post #43
post #28

Earlier quoted context omitted.

>I am dreading the technical debt I have in test-unit, and meanwhile the (Rails) world is moving on to Minitest Minitest and Test::Unit are part of the same package since 1.9, with Test::Unit implemented as a simple compatibility layer on top of Minitest: http://www.rubyinside.com/a-minitestspec-tutorial-elegant-sp... The Minitest assertions are quite similar to the old Test::Unit assertions, a large number are ident…

Right, and I've seen many pages like the rubyinside one leaving me still hungry with questions like, "So, no more test_helper.rb or what?" Couldn't find that in either of those links. This is really more of a general documentation quibble on my part though, since I'm the dope who can't read through my questions and find the answers in the source (or wherever). I've been planning on making a blog post about 1:1 mappin…

The docs really are lacking in this case; I expected the Test::Unit docs would document what's being stubbed and not in MiniTest but there's nothing there on that as near as I can tell.

As a rule, I miss the thoroughness of CPAN/Perl docs. Some ruby packages have amazing docs but often I feel at sea, particularly with regard to high level overviews and examples.

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

#48

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

neat, I didn't know you could do that. thanks for sharing!

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

#49

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…

I didn't understand why the author used the unnecessary regex assertions, which are less clear and don't add any additional checks beyond:

  article.slug.should == "testing-with-test-unit"

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

#50
post #44

Earlier quoted context omitted.

"because Test Unit documentation is terrible" Are you confusing Test Unit documentation with 'How/What to test' documentation? I can't think of any documentation required if what you use is a group of asserts. Test::Unit in a nutshell: `test "should do something"; ...; end` is what gets run in each of the files. `setup` is called before each test. `teardown` is called after each test. use `assert_*` at your discretio…

You know, recently I had reason to look into the teardown method (DatabaseCleaner, dontchaknow), and I never did find anything conclusive that it was something that test-unit used. Sure, it was mentioned in many many DatabaseCleaner questions/answers/threads, but nothing I could really rely on since invariably these were also using Rspec. That is, my unanswered question was "Is teardown solely an Rspec thing?"

Setup and teardown are in the Rails testing guide.

http://guides.rubyonrails.org/testing.html#setup-and-teardow...

Post reply on HN