burke / zeus

Boot any rails app in under a second.
MIT License
3.33k stars 231 forks source link

zeus test not working with minitest-spec-rails in Rails 4 #545

Open speckins opened 8 years ago

speckins commented 8 years ago

OS X 10.9.5 Ruby 2.2.1 Rails 4.2.6 zeus 0.15.4

I'm using the minitest-spec-rails gem to enable the Minitest::Spec::DSL in my Rails tests. I can run spec-style tests when I use "rake test", but when running tests with "zeus test," they raise errors.

In my application, I'm getting NoMethodError: undefined method 'assert_difference' for #<#<Class:0x007fc16604e8e8>:0x007fc165598d90> and NoMethodError: undefined method 'users' for #<#<Class:0x007fc16604e8e8>:0x007fc1655db078> errors. (The undefined method 'users' is referring to a fixture helper.)

require 'test_helper'

class UsersControllerTest < ActionController::TestCase

  describe 'specs' do
    let(:user) { users(:test_user) }

    describe 'creating a new user' do
      it 'creates a new user' do
        assert_difference('User.count') do
          post :create, user: {  }
        end
      end

      it 'redirects to the user path' do
        assert_redirected_to user_path(user)
      end
    end
  end
end

I checked ActionController::TestCase.ancestors in both cases, and when running with "zeus test", the following ancestors are missing: MiniTestSpecRails::Init::ActionControllerBehavior, MiniTestSpecRails::DSL, Minitest::Spec::DSL::InstanceMethods, MiniTestSpecRails::Init::ActiveSupportBehavior.

I have tried to reproduce this in a new Rails 4.2.6 application, pulling in only "minitest-spec-rails," and I get an undefined method 'it' for ApplicationControllerTest:Class (NoMethodError) error.

class ApplicationControllerTest < ActionController::TestCase
  test 'the old way' do
    assert true
  end

  it 'works like new' do
    expect(true).must_equal true
  end
end

I suspect I need to add something to the custom_plan.rb file, but I'm not sure what. Could this be related to the minitest-spec-rails Railtie's conditional on Rails.env.test??

I've been using zeus and "minitest-spec-rails" successfully together in a Rails 3 app, but Rails 4 is having none of it.

speckins commented 8 years ago

Yes, this was because of minitest-spec-rails checking Rails.env.test? when conditionally defining initializers.

Using the following fixes my issue:

# custom_plan.rb
class CustomPlan < Zeus::Rails
  def test_environment
    ::Rails.env = ENV['RAILS_ENV'] = 'test'
    super
  end
end

Zeus.plan = CustomPlan.new

Rails.env.test? needs to be true before calling Bundler.require(:test). I'm not sure why they're enforcing initialization only when the Rails environment is "test". It seems like that should be handled by putting the gem in the :test group in the Gemfile.

Would it be appropriate to move the RAILS_ENV line before the Bundler.require line in both test_enviroment and development_environment in zeus? If so, I can generate a patch.