Rspec multiple spec folders - ruby

Im currently building a project using Rspec in which I sepparated in two different folders with domain code and infrastructure code. Both folders have their own specs in a spec folder. The domain/spec folder is the one containing the spec_helper.rb file, thats required from the tests inside the other folder infrastructure/spec
I'd like to know how to have a spec folder in the root of the project, including the spec_helper file and also tests, and being able to run all the tests with just one command (right now I do it running rspec domain/ infrastructure/)

RSpec is designed to work with all tests in one folder. By default, this folder is called spec/, but you can use a different name with the --default-path option.
So, your options as I see it are:
Edit the source code of rspec-core to let that configuration support multiple directories. Hopefully your PR will be approved and merged.
Or, write a simple wrapper script that runs rspec against both directories. For example, you could alias rspecs='rspec domain/ infrastructure/'.
Or (what I would recommend!), you could just restructure your tests slightly to use spec/domain/ and spec/infrastructure/ folders -- and then everything will just work, by convention, out of the box.

Related

Ruby - rspec how to include specs in root of project?

rspec always finds files in the spec/ directory tree.
How can I have it also find files that are in the root of the project (the one that contains the spec/ folder iself.
For example if I have a small project with only two tests, any folders may essentially be unneeded overhead given a minimalist approach. Or if you only have one type of test and would only create one folder it may not (or may) be worth creating that one folder or just omitting it altogether. The value it adds of course is immediate description of what type of tests exist, even if only one type exist.
Spec supports a --default-path option, and you can put a default set of command-line options into an .rspec file. That is, you can create a .rspec file in your project root directory containing
--default-path .
My experience has generally been that rspec tests are almost always in a spec directory; I would be a little surprised to see them in a top-level directory. This in particular mirrors the standard gem filesystem layout which puts all library code into a lib directory and tests in a parallel spec directory.
One approach:
# File: spec_helper.rb Add the following at the top:
Dir.glob(File.expand_path("../../*_spec.rb", __FILE__)).each do |file|
require file
end
This will include files at the root to the ones already being included in spec/

Where to put test files that get created. Ruby directory structure

Say I'm writing an integration test that reads in a CSV and outputs a CSV with some changes in the data. Where do I put this test file in a Ruby project? Is there some convention for where temporary files that get created in tests go that eventually get torn down or delete?
Say my project structure is simple:
--root
--app
--promotions
--spec
Gemfile
Gemfile.lock
Sounds like a perfect candidate for Dir.mktmpdir.

Good project layout for a ruby system project?

I am writing small Ruby program that is used to backup some folders and send to s3,
e.g.
backup.rb --folder /folder1 --folder /folder1 ...-c config.ini
Before I start writing this program from scratch, I want to know if there is some existing good sample project layout that I can reference?
e.g. how to layout the tests, libraries, classes, configs, doc etc
I am particularly interested in a layout that allow me to share the libraries so later I can easily re-use in other project., even submit to ruby gems
Would be helpful if anyone can send me some good examples from existing OSS.
This is a pretty standard structure. Drawn from RubyGem Tutorial
.
bin/ # Executables here
data/ # Assets here
doc/ # RDOC for the lib directory
lib/ # Code for the project
project_module.rb # Contains project module
project_module/ # Classes for project module
# Either
spec/ # RSpec Tests
test/ # Test::Unit Tests

Capybara + RSpec, spec/features dir being ignored by the rspec . command?

I'm using the new Capybara DSL with rspec following the short guideline located here
And the tests added to the spec/features directory run well alone, ie.
rails_project$ rspec spec/features/my_first_feature.rb
However the spec/features directory is totally ignored when I try to run the tests for the entire spec/ directory like this:
rails_project$ rspec .
There is no mention in the guide about how to include this directory (or other directory) to accomplish this inclusion that I desire. I really need this for continuous integration of my project.
Can anybody please tell me how to do it?
Thanks!
You need to make sure all of your specs end in_spec.rb.
Change the filename to spec/features/my_first_feature_spec.rb
This is how I do it:
rspec spec
You may also want to use guard-rspec, which gives you better control.

Ruby - Where to put resource files that a module in lib relies on?

I am trying to structure my ruby project following best practices. I currently have something like this:
test_project/
bin/
test_project # My Executable
lib/
test_project/
my_module.rb
test_project.rb # Loads my_module.rb
I setup it up this way based on recommendations I found on the web.
My problem is I have some resource files, "resouce1.txt" and "resouce2.txt". My executable needs to open the file "resource1.txt". my_module.rb needs to be able to open the file "resource2.txt". Where do I put these plan text resource files in this directory structure and how to I open them (File.open) from the corresponding ruby files.
Since /lib has to do with specifically the Ruby that powers your gems, I would put it in a top-level directory in your gem named after their subject matter. If they are files with lists of species I would call it /species.
You could also go the Rails way and put it in an /assets folder if you have a lot of external assets like /assets/species. Either way, I would not be prone to put them in /lib.
I don't think there is a standard place for these, as for the most part it is operating system dependent (/var and /etc vs Program Files vs the Application Bundle). But your best bet (I think) is to either put them in the root of your hierarchy, put them in lib/ or, if they really are static text files, put them in your script.

Resources