If you are here, maybe it is because you also like fast tests!… I really like fast tests, and for that reason (and some others…) I always try to decouple my code from Rails and other frameworks…

But, the reality is that if you are using Rails, you also need to write “Rails code”…

If you are like me and you simply cannot work if you need to wait for Rails to start… And you need to write tests for a Rails helper function, then you can follow this steps and write your helper function doing TDD with fast tests…

1. Do not require the rails_helper

In your test file instead of writing at the top…

require "rails_helper"

Try to require the file that you are testing and the stuff that you need.

For example If you are working in a function that will use the number_to_human helper. You could require just active support, instead of requiring all rails. Like this…

require "active_support"
require_relative "../../app/helpers/formats_helper" # this is the helper you are testing

2. Add a class that includes your helper and the helpers that it needs

In your test file inside the RSpec.describe block you can add a class an include the helpers that you need and the helper that you are testing.

Following the same example… If your function will use the number_to_human helper and that helper lives in the ActionView::Helpers::NumberHelper module, then you need to include that module and the one you are testing, like this…

require "active_support"
require_relative "../../app/helpers/formats_helper"

RSpec.describe FormatsHelper, type: :helper do # as in the specs https://relishapp.com/rspec/rspec-rails/docs/helper-specs/helper-spec
  class Helper
    include ActionView::Helpers::NumberHelper
    include FormatsHelper
  end
end

3. Expose a helper method

Now just create an instance of that class, and expose it to your test…

Like this…

require "active_support"
require_relative "../../app/helpers/formats_helper"

RSpec.describe FormatsHelper, type: :helper do # as in the specs https://relishapp.com/rspec/rspec-rails/docs/helper-specs/helper-spec
  class Helper
    include ActionView::Helpers::NumberHelper
    include FormatsHelper
  end

  attr_reader :helper

  before do
    @helper = Helper.new
  end
end

4. Write your function using TDD with fast tests =)

Now you can write your helper using TDD with fast tests.

require "active_support"
require_relative "../../app/helpers/formats_helper"

RSpec.describe FormatsHelper, type: :helper do # as in the specs https://relishapp.com/rspec/rspec-rails/docs/helper-specs/helper-spec
  class Helper
    include ActionView::Helpers::NumberHelper
    include FormatsHelper
  end

  attr_reader :helper

  before do
    @helper = Helper.new
  end

  it "transforms 1234 into 1K" do
    expect(helper.short_amount_format(1234)).to eq "1K"
  end

  #...
end

5. If you want, return to the default way

After you have finished, you can return the stuff that we added to let the file as in the docs

require "rails_helper"

RSpec.describe FormatsHelper, type: :helper do
  it "transforms 1234 into 1K" do
    expect(helper.short_amount_format(1234)).to eq "1K"
  end

  #...
end

And that’s all for now =) … I hope you can use it and feel the difference!