Where do rspec tests for code under lib/ go? - ruby-on-rails

I've got some code in the lib/ directory that don't really belong under controls, models or helpers. I'd like to write some rspec tests for this code, but am not sure where they should go under the spec/ directory. Is there a convention that's commonly followed?

I would use a subdirectory of /spec as well. However, I would consider refactoring the code into a separate plugin or gem if it is really separate and can be tested standalone. In this plugin, you can create a separate spec-directory.

Related

Why use /app/lib instead of /lib in Rails?

In the sidekiq documentation, there is this quote about preferring to use /app/lib instead of /lib in Rails projects related to autoloading errors:
A lib/ directory will only cause pain. Move the code to app/lib/ and make sure the code inside follows the class/filename conventions.
Additionally, there is also:
Don't configure extra paths in autoload_paths or eager_load_paths. That's a hack; follow the conventions! Any directory underneath app/ may contain Ruby code, you don't need to explicitly configure anything.
My questions are:
Is there any truth to these statements that using /app/lib is better than /lib?
Is this only helpful for autoloading Rails-related objects (such as AR models, controllers, jobs, etc)? Or will it also help POROs?
Is there only a specific context in which these comments make sense?
In my experience app/lib is easier to use. You can literally stick in something like Class MathFunction and use it elsewhere (e.g. controllers or modules) with MathFunction.sqrRoot.
To use /lib you need to configure your Rails app with autoload_paths. autoload_paths also need some tweaking to work properly in production. Matz himself discourages autoload because it's in the process of being deprecated.
The only time I've needed to use the lib directory is for making custom rake tasks. Otherwise I stick to app/lib.

Importing Node Modules in to Rails

I'm working with Jasmine. I spotted this handy looking library: https://github.com/JamieMason/Jasmine-Matchers and I thought its collection of customer matchers would help me a lot.
Problem is, it's loaded with files common to Node applications, such as JSHint, Grunt, travis.yml etc
The project I'm working on, that would love these matchers, is a Rails application. I've tried dropping them into my assets/javascripts and requiring in application.js, but obviously, life isn't that simple.
What is the correct way to install these files, and integrate them with Jasmine in a Rails context? Is Bower the tool for the job? If so, what's the right procedure to adding JS dependencies/integrating them off the bat?
Author of Jasmine-Matchers here, the only file you need to load into your test environment is this one https://github.com/JamieMason/Jasmine-Matchers/blob/master/dist/jasmine-matchers.js.
The other files are part of the development repo, I'll open an issue to have those excluded from npm/bower packages to save confusion.
You should be able to copy that file to your assets/javascripts directory, then embed it after Jasmine but before your tests.
Please comment if I've missed anything out here.
I was also trying to use Node modules inside my Rails application and the easiest way I found to achieve it was through browserify-rails gem.
The installation is pretty straightforward, just follow the getting started section and everything should be working. :-)

Why is there Rails.rb files all over the place?

Was digging around my Rails applications and noticed that there are rails.rb files all over the place. In my ruby gems directories like:
...gems\devise-2.0.4\lib\devise\rails.rb
...gems\cucumber-rails-1.3.0\lib\cucumber\rails.rb
...gems\railties-3.2.3\lib\rails.rb
I am assuming that there are executed whenever you issue some command like "rails xxx". So all these extra rails.rb files combine with the original rails.rb file to essentially make one big rails.rb file. Essentially, when we type in "rails xxx" it goes thru all them all?
Just looking for some confirmation PLUS a little more knowledge about this. Thanks.
The best way to understand what these rails.rb files are doing, is to read the source code.
ralties
devise
cucumber-rails
As you can see, in any library the file assumes a different scope. The common behaviour is that the file rails.rb normally contains the code required to initialize the library when loaded from a Rails project.
BTW, this has nothing to do with the script/rails command and there is no "big rails.rb" file.
The files are not generated but are simply source files of these libraries you are using.
In this case they are probably rails-related classes that either extend Rails in some way or modify it or make the library interact with Rails.
Rails is a very common framework in Ruby land so most if not all libraries will have some sort of integration with Rails.
By no means are all of those loaded when you run rails XXX but rather when your application loads these libraries their rails.rb files may be executed to provide some sort of integration with Rails.

Can/should directories for test frameworks be consolidated into a top-level directory (a la vendor/plugins)?

For some reason, I find it really irksome that the files for each testing framework (rspec, test::unit, cucumber, etc.) live in a separate folder in the top level of my app. Is there a strong reason these directories should be scattered about instead of consolidated like gems/plugins in the vendor directory?
If there isn't an actual reason for the way it is, would it be kosher to consolidate these directories into a top-level "test" directory, containing subdirectories for each of the testing frameworks alongside fixture data, etc.? Has anyone else ever been driven bonkers this (and maybe already hacked together a quick'n'easy way of updating the necessary paths to get this to work)?
I for one would welcome this change.
Personally I use RSpec and Cucumber at the same time and I'm torn whether or not to put the fixtures in the features/support/fixtures directory (I create it myself) or spec/fixtures. What if I use this fixture in features and in spec tests too?! [overly emotional] Why won't somebody think of the fixtures!!
Definitely. A top-level test directory with the subdirectories of features, specs, fixtures and whatever else you need.
Great question!

How to test lib files in Rails?

I understand the benefit of putting classes, modules, etc. in the lib folder in Rails, but I haven't been able to find a clean way of testing these files. For the most part, it seems like unit tests would be the logical approach.
I guess my question is: What is the "rails way" for testing lib files?
Your lib directory is not automatically loaded by rails.
You can use ActiveSupport::Dependencies to override const_missing. Basically rails will try to load your constants when it boots, if they are undefined or not in memory it will look at your load paths.
If you have a file like my_class.rb, rails expects it to be MyClass.
The beauty of this is if you have some stuff in your lib directory, you don't have to require it with a relative path you can just say require 'something', instead of require 'lib/something'.

Resources