0

To tidy up codes, I separated RSpec tests for concerns methods to spec/concerns directory.

I followed this answer. (Copy & Paste from the answer)

# app/models/concerns/personable.rb

module Personable
  extend ActiveSupport::Concern

  def full_name
    "#{first_name} #{last_name}"
  end
end

# spec/concerns/personable_spec.rb

require 'spec_helper'

shared_examples_for "personable" do
  let(:model) { described_class } # the class that includes the concern

  it "has a full name" do
    person = FactoryGirl.create(model.to_s.underscore.to_sym, first_name: "Stewart", last_name: "Home")
    expect(person.full_name).to eq("Stewart Home")
  end
end

# spec/models/master_spec.rb

require 'spec_helper'

describe Master do
  it_behaves_like "personable"
end

# spec/models/apprentice_spec.rb

require 'spec_helper'

describe Apprentice do
  it_behaves_like "personable"
end

When I edit personable_spec.rb guard detect the update, but because there is no example in the file it ends up No examples found.

I have to run all spec to test the personable_spec.rb. Is there a way to test examples automatically that are defined in shared_examples_for method?

Community
  • 1
  • 1
ironsand
  • 14,329
  • 17
  • 83
  • 176

1 Answers1

0

This is what you have to do:

1- Do not name your shared example files with the suffix _spec, they are not spec file they are support file. I also suggest to group them into a sub-folder named support for better file organization.

So move your file spec/concerns/personable_spec.rb to spec/support/concerns/personable.rb, or even spec/support/shared_examples/concerns/personable.rb, or what ever you find suitable.

2- Remove the line require 'spec_helper' from the suport files, this require statement is only required in spec files, not in support files

3- Tell rspec to load all files under your support by adding at the end of spec_helper.rb file:

# Load custom matchers and macros, etc
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

4- Restart guard and run your test.

5- You also need to tell guard to look for change in all files located under the support folder and sub-folders, so you won't have to restart guard for every change

Benjamin Bouchet
  • 12,971
  • 2
  • 41
  • 73