How to run spec for a gem in your path - ruby-on-rails

The Problem:
I have a Rails application and I can successfully run the RSpec suite using rspec spec. I've also made a neat little gem that also has a pretty little copyright next to it. Its not open source. Its a gem that belongs to that Rails application and that Rails application only.
Now, this gem has some specs and it also pumps out a nice string of green. So, to test the whole application I might do:
$ rspec spec
$ cd custom_gem && rspec spec
That's nice, but what if I want to run the whole suit from one line? Well I could do:
$ rspec spec custom_gem/spec
But, this doesn't work in my case because the gem is also a Rails engine. Due to this, it has to connect to a dummy application within the gem.
Now that you know this, I can finally ask my key question:
How do you run a spec for a rails engine and the spec for your main application given that your rails engine should also have a dummy application to test upon?

If both of them are running correctly on their own, why not just make a bash alias?
alias test_all='cd /full/path/to/gem && rspec spec && cd /full/path/to/rails && rspec spec'

Related

Need to execute a script prior to running Rails specs on a gem

I have a gem that relies on the database of another rails application.
In order to run the gem locally, I set up a symlink in the gem's test/dummy/db to the schema.rb in the application. Works great.
Recently there were some changes to the application's schema.rb to add lines like:
create_function :current_master_org_id
The gem complains when running the specs that it doesn't know what create_function is.
The gem doesn't need to know anything about the functions, I could run a script like:
sed -e '/create_view/{N;N;d;}' test/dummy/db/schema_original.rb | sed -e '/create_function/{N;d;}' > test/dummy/db/schema.rb
I'm wondering what a good solution to this problem could be? Could I add a Rubymine "action" or something to scrub the schema.rb file prior to running specs? What if someone wanted to run specs and didn't use Rubymine?
Any suggestions on how to get around this?

Running bin/rails generate don't work

Im following a rails tutorial, and when im supposed to run the command: 'bin/rails generate model Article'. An error occurs saying that there isnt such a command.
I'm using 'command prompt with ruby on rails' and in the rails project i can find a Bin folder. Am also using windows 7.
Also What is the difference between running only 'rails generate' instead of running 'bin/rails generate'?
Using rails generate is fine if you have no bin stubs (binaries in the root /bin folder of your project). If you do have bin stubs then it's preferred to use them because they may do additional things specific to your project. But even then, it's (probably) fine to just use rails generate still. The other bin stubs may be a little more necessary to use, though (again, if present) because they tend to be shortcuts to e.g. bundle exec rake.
Rails 4.1 ships with bin stubs. That is, when you generate a Rails 4.1 project it generates bin stubs for you. So this is probably why your tutorial mentioned using them -- they're now there by default. But if you're on an older version of Rails that won't help you much.
The big reason Rails 4.1 includes bin stubs is because Rails uses spring by default now. Spring is an application preloader... that makes it so that when you call e.g. bin/rake ... it will load and keep a running rails environment in the background and then, the 2nd time you call bin/rake it will fork from the running environment giving you almost instantaneous response. So this is an example of "additional things specific to your project" that you get from using bin/rake over just rake and bin/rails over just rails.

What is the logic behind tasks in Rake versus under `rails`

I try to grasp the logic behind some tasks being "rails" whereas others, the majority, is found as rake task. Why rails server and not rake server for example?
I can understand that the bootstrapping cannot be done in rake: after all, you first need a rakefile and other requirements before you can start using rake. So creating the project with a rails binary seems only practical.
But why generate, server, console, yet not migrate or assets? I don't see the logic. Is there any?
IMO the rails scripts are for "live" console usage, like during development.
The rake tasks are more "automated" tasks, for example, that might be run as part of a build or deploy cycle, like by a CI server. Some rake tasks might group rails/etc. commands together (like tests).
A rake script is a utility/build tool for some common tasks when developing. For example, you need to do deployment, run test, database stuffs, truncate log files, compile assets .... You can create your own custom rake scripts.
A rails script is ruby file located under script directory for the purpose of the gem rails. This is what the gem does. Rails is a ruby web framework, so the command rails is for starting the rails apps, go to rails console, generate files. It's bundled when you install the gem.
You can think of rails command like bundle command for bundler. bundle install, bundle update ... all are related to resolving gem dependencies. rspec command for running tests...
Some gems has an executable script such as rails, bundler, capistrano, whenever, rspec. Some other gems doesn't have such as builder, will_paginate....
You can check this out for how to add executable to a gem, http://guides.rubygems.org/make-your-own-gem/#adding-an-executable

Cucumber: web_steps.rb

I'm wasting my time here and I can't seem to figure this out..
I have used Cucumber in Rails applications before, and if I'm not mistaken, it generates the features/step_definitions/web_steps.rb file when you run rails g cucumber:install. Right?
I looked this up in a book I was using a while ago to learn Rails and it says so there aswell:
It nevertheless passes because of the features/step_definitions/web_steps.rb
file, which was generated when you ran the rails generate cucumber:install command.
However, when I run it in this application I'm trying to start working on, it does not generate it..
$ rails g cucumber:install
create config/cucumber.yml
create script/cucumber
chmod script/cucumber
create features/step_definitions
create features/support
create features/support/env.rb
exist lib/tasks
create lib/tasks/cucumber.rake
force config/database.yml
No web_steps.rb to be found. Am I losing my mind here?
Thanks.
Which version of cucumber are you using? if it is a recent version, see
https://github.com/cucumber/cucumber-rails/blob/f027440965b96b780e84e50dd47203a2838e8d7d/History.md

How do I run my Specs with the previous version of Rspec?

I have been following an RSpec tutorial on one of my machines, in the hope of learning more about BDD and TDD. My setup was with Rails 2.2.2 and Rspec 1.1.12
Tonight I decided to continue on my primary machine and moved my code from my portable to my desktop. Not having RSpec, i installed the gem . . .
sudo gem install rspec
sudo gem install rspec-rails
Strife and Calumny! The new version of Rspec installed! 1.2.0! And now my tests are failing all over the place! While I fully intend to follow up and learn the most up to date version, I would really like to complete what's left of the tutorial without having to start over. I am wondering. Is there a way to install and specify that I would like to run my code against the previous Rspec, 1.2.12?
You could uninstall and reinstall with VERSION specified. Explained here.
If you want to have more than one version on your computer, for if maybe you should have a 2.2 rails app and a 2.3 rails app then in your environment.rb file specify:
config.gem, 'rspec', :lib => 'spec', :version => '1.1.12'
and your application will use that gem spec and the rspec-rails gem that goes with it. This will enable you to use the appropriate gem for each appliction.
In addition to specifying the rspec and rspec-rails versions in my environments/test.rb file, I added script/ in front of spec, e.g.:
script/spec spec/controllers/treasury_accounts_controller.rb
to get past this error:
/opt/ror/ruby-ee-1.8.7-2011-03/lib/ruby/site_ruby/1.8/rubygems.rb:335:in `bin_path': can't find executable spec for rspec-2.1.0 (Gem::Exception)
from /opt/ror/ruby/bin/spec:19

Resources