Should Matchers be automatically included in ActiveSupport TestCases? #19

Closed
txus opened this Issue Sep 22, 2010 · 15 comments

2 participants

@txus

Hi,

Trying to use test/unit with only rspec-expectations has lead me to a question: if just requiring "rspec/expectations" in your test_helper.rb properly extends Kernel with "should" and "should_not" methods (enabling you to do number.should == 3), Matchers module only includes its methods in RSpec.

In order to use the Matchers now you have to include RSpec::Matchers inside ActiveSupport::TestCase class, in test_helper.rb. But this isn't obvious, as expectations work out-of-the-box (as they extend Kernel once required). Why not include Matchers in ActiveSupport::TestCase as you require 'rspec/expectations'? It could be something like:

module Matchers
    if RSpec.respond_to?(:configure)
      RSpec.configure {|c| c.include self}  # This is what it does now
    end
    if defined?(ActiveSupport::TestCase)
      ActiveSupport::TestCase.send(:include, self)
    end
end

Would that be too obtrusive for an app using both test/unit and rspec? Or is it reasonable to assume that when you require rspec/expectations you definitely want the matchers too?

@dchelimsky
RSpec member

I think it's reasonable to assume that you'd want both matchers and should/should_not, but I would want to make that work in Test::Unit::TestCase and/or Minitest::TestCase, not ActiveSupport. Make sense?

@dchelimsky
RSpec member

No specs?

@txus

I'm trying to spec this (my spec in http://gist.github.com/607800), but for this to work there must be both Test::Unit::TestCase and MiniTest::Unit::TestCase defined before loading rspec/matchers. The thing is, no matter where I define these classes (in the spec, in spec_helper before everything...) they got defined after rspec/matchers is required, so that it won't extend them properly.

How would you force 'rspec/matchers' to re-load? Or is there another way of doing this?

@txus

Yes, but my issue was with the spec rather than the implementation (I updated the gist http://gist.github.com/607800 to show it more clearly).

The thing is that in the spec I'd like to force "re-loading" of rspec/matchers, so that it extends the classes defined in the same spec.

@dchelimsky
RSpec member

@txus - sorry - I had a different issue in mind when I wrote that last comment (the one from rspec-mocks).

You can reload using load instead of require. Not sure what else that might break, but give it a shot.

@txus

Looks like it was an issue with the paths. It's working no :) Does it look ok?

http://gist.github.com/607800

@dchelimsky
RSpec member

Looks fine - go ahead and push it to your repo and i'll merge.

@txus

Include Matchers in MiniTest and TestUnit

@dchelimsky
RSpec member

I had to change the require to load in the spec, but otherwise all is well.

Merged: 78605a8

@txus

Ok!

@dchelimsky
RSpec member

Ha! Github finally got it working where it closes the issue automatically even if I'm not the listed author. Sweet.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment