sporkrb / spork-rails

Spork plugin for rails
MIT License
184 stars 45 forks source link

Spork not reloading code properly in Rails 4.1.0.beta1 #22

Open gabrielecirulli opened 10 years ago

gabrielecirulli commented 10 years ago

I've been working on a new Rails 4.1.0.beta1 app (started as such, not upgraded from a previous version), using Ruby 2.1.0, and Spork is giving me some issues.

From my Gemfile.lock, I'm using spork-rails (4.0.0) together with guard (2.4.0), guard-rspec (4.2.6), guard-spork (1.5.1) and rspec (2.14.1).

When I update a model spec and save the changes, it runs correctly (the latest version of said tests is run), but whenever I edit a model and save, Guard detects the change and runs RSpec, but the tests are run against the old version of the code. The code doesn't seem to get reloaded unless I manually restart Spork (which goes against its entire purpose).

In config/environments/test.rb I have the following two settings:

Rails.application.configure do
  config.cache_classes = true
  config.eager_load = false
end

These are the default settings that I never changed in any of the other Rails 4.0.0 stable projects, but even when changing them I don't get any significant changes in Spork's behavior.


Here is a practical example of this issue.

I have a simple User model:

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :confirmable, :lockable

  validates :username, presence: true, format: { with: /\A[A-Za-z0-9][A-Za-z0-9.\-_]*\z/ }
end

And the following tests in spec/models/users_spec.rb:

require 'spec_helper'

describe User do
  let(:user) { create(:user) }
  subject { user }

  describe "with an invalid username" do
    it "should be invalid" do
      invalid_usernames = [
                            "", "john/doe", "john&doe", "john@doe", "john^doe",
                            "-john", "_john", ".john", "john doe"
                          ]

      invalid_usernames.each do |invalid_username|
        user.username = invalid_username
        user.should_not be_valid
      end
    end
  end

  describe "with a valid username" do
    it "should be valid" do
      valid_usernames = [
                          "john_doe", "john.doe", "john-doe", "john-doe123",
                          "j", "1337doe", "1-2-3"
                        ]

      valid_usernames.each do |valid_username|
        user.username = valid_username
        user.should be_valid
      end
    end
  end
end

These tests pass correctly when run, but as soon as I make any change in the model that would obviously stop the tests from passing, such as validates :username, presence: true, format: { with: /\A\z/ }, Guard will detect the change, re-run the tests and they'll still be passing, making it seem like the code has not been reloaded by Spork.


This might be a problem caused by a change in Rails 4.1.0.beta1, because I never encountered this when using other stable versions of Rails.
I'll be trying to downgrade the app I'm working on to see if I can reproduce this issue, also because I need to proceed and this problem is currently making my workflow harder.

gabrielecirulli commented 10 years ago

I've been downgrading my application to Rails 4 and I'm still encountering this problem. Right now, Spork doesn't seem to be reloading application_helper.rb.

gabrielecirulli commented 10 years ago

This behavior seems to stop if I do the following:

It only works when both these things are there. It does not work when I do only one of the two.

This is very strange. Is this a known problem? Could you help me figure out what's wrong?

nathany commented 10 years ago

Maybe the gemspec should limit to rails < 4.1 for now?

jmstone617 commented 10 years ago

I am using Rails 4.1 and Ruby 2.1.1. I fixed the reloading issue by specifying

gem 'spork', '~> 1.0rc'

in my Gemfile, and then running

bundle update

in order to update the .lock file.