metaskills / minitest-spec-rails

:bento: Make Rails Use MiniTest::Spec!

Home Page:http://github.com/metaskills/minitest-spec-rails

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why Is MTSR Needed For Rails 4.1?

metaskills opened this issue · comments

I think the question is why is MTSR needed for a Rails 4.1 application. I am going to use this basic example to talk around https://github.com/metaskills/mtsr_integration

Love this! I was under the impression that including minitest/spec in the test_helper.rb gave access to the spec DSL, no?

If so, that would be wonderful news to me and means the end of live of this gem could be near. One sec, making some commits to that project...

I took the project referenced in the article and added this to test_helper.rb:

require "minitest/spec"

class ActiveSupport::TestCase
  extend MiniTest::Spec::DSL
end

And the spec style worked. Are there things this doesn't cover?

Step 4 in this perhaps? (re: http://blowmage.com/2013/07/08/minitest-spec-rails4)

See the commits on this branch, I also gave you collaborator access.
https://github.com/metaskills/mtsr_integration/tree/mtsr_issue_48

I see, so without:

class << self
    remove_method :describe
  end

  register_spec_type self do |desc|
    desc < ActiveRecord::Base if desc.is_a? Class
  end

The describe syntax wouldn't work:

describe User do
end

extend MiniTest::Spec::DSL

OK, yes... I knew you had to be doing that. Short answer is yes, I think there will be some things missing. Let me see if I can for that mtsr_issue_48 and illustrate,

The describe syntax wouldn't work:

Yup, that one. But not a fan of that spec style... but yea, that is one. I'll illustrate a few more...

I think I see what's going on and definitely appreciate the detail. I'm a little surprised it's not easier, but I guess that's the point of this gem!

I suppose the only other way would be to bake it in to Minitest, which doesn't seem right, so I think all is well. Until Rails decides to have it internally, which is probably never.

OH, I remember a BIG point!!! The default Rails behavior of tests, all the way back as far as I can remember is that setups and teardowns are callbacks that are stackable. This is really helpful when libs/gems/modules add additional setup/before or teardown/after hooks. They just get added to the stack. This gem allows you to maintain that functionality too and delegates MiniTest's before/after to that exact same callstack. This is a big deal if you are upgrading existing Rails applications and/or run into crazy situations where MT and Rails are on different pages.

☝️ That is no small statement either. I wish I could recall some of the crazy things you see in larger tests, especially capybara ones when ActiveSupport's setup and teardown are on different levels. This is why I test this gems delegation in a few places, even in subclasses/describes. For example:

https://github.com/metaskills/minitest-spec-rails/blob/master/test/cases/active_support_test.rb#L28

This gem also exists so people can have a solid upgrade path from Rails 2.3 to 4.1. Not everyone can come fresh into 4.1 and even if they did, there are slight integration issues that this gem still solves. Ones that will waste half your day sometimes when you hit them.

I see! Always seems like that last 20% takes 80% of the effort. Thank you for the detailed explanation. I do get it now. Because I haven't been using the spec syntax, I took for granted how easy the default stack is an assumed it was just as easy. I now know that's not the case.

Ahh... good example of how Rails stacks things on the setup/teardown chain. Most gems do this, if you just go raw spec DSL include, you won't be able to keep things in sync. Rails may setup or teardown too early or too late.

included do
  class_attribute :_mailer_class
  setup :initialize_test_deliveries
  setup :set_expected_mail
end

That makes sense. I added a teardown to clear out the mailers on my suite as well.

Thanks @brandonhilkert - Maybe you can make a mention to this in your post. I really think MTSR is the only gem that champions pure Rails integration with just a minimal amount of loose end patches that will help both new and seasoned developers get up and working.

I'm working on my new blog layout now and promise to do a full write up on how fixtures can be awesome when your models drive and talk about named_seeds too. Again, thanks for taking the time to chat today.

Of course. Loved it.