RSpec naming conventions for files and directory structure

前端 未结 2 1268
慢半拍i
慢半拍i 2021-01-02 02:05

I\'m writing RSpec tests and I have come to a point where I am not reading the same opinions on different websites. The directory structure for RSpec is clear when we are d

2条回答
  •  醉梦人生
    2021-01-02 02:45

    The files read by the rspec gem are simply those that end in _spec.rb and that are anywhere in the hierarchy below the spec folder as long as the top of the hierarchy is an alpha-numeric word (that is, files under a folder named spec/##/ would not be considered in generating specs.) This is the relevant line of code that implements this, in /gems/rspec-rails-2.14.1/lib/rspec/rails/tasks/rspec.rake:

    namespace :spec do
      types = begin
        dirs = Dir['./spec/**/*_spec.rb'].
               map { |f| g=f.sub(/^\.\/(spec\/\w+)\/.*/, '\\1') ; puts ">>> Found #{g}."; g }.
               uniq.
               select { |f| File.directory?(f) }
        Hash[dirs.map { |d| [d.split('/').last, d] }]
      end
    

    So all the text in the filename previous to _spec.rb is a convention - it doesn't change how Rails processes the files.

    I'd have to read through the code a lot more to figure out what the significance of each folder name, under spec, is - it gets passed through to the Rake spec task that's created, so it's being used for something but I don't know what.

提交回复
热议问题