Including Rails engine tests with autotest - ruby-on-rails

Just wondered if anyone knew of a great way to include rails engine tests into autotest? Basically when I run autotest, I would like to have all of my app tests run first, then have the tests run in my engine. I noticed this gist after some googling: Autotest for Engines Seems kind of like a lot to have to go through - wondered if there was something simpler I was totally missing.

Wait, so that works but you don't want to use it?
If you're worried about having it clog up your .autotest you could name it something else:
Save it as vendor/autotest/autotest_engines.rb
Then from within your .autotest:
require 'vendor/autotest/autotest_engines'
P.S. It's not really that big compared to the autotest/rails gem, and you're probably requiring that too:
https://github.com/grosser/autotest-rails/blob/master/lib/autotest/rails.rb

Related

Testing legacy app in RoR - skip the DB tasks

I'm a PHP dev mostly and just starting out with Ruby, so please correct me if I say something dumb.
I'm working on fixing some bugs in a "legacy" app written in rails. The app itself has never been unit-tested. I can see the test scaffolding generated by rails but tests are nowhere to be found.
The app is pretty big and the code quality is very bad, so I wanted to write some units tests for the functionality I'll be fixing, before writing any code.
The problem is that when I run rake test command, the testing DB is created, but if I write any tests it keeps crashing on me. There are several problems with some relations and keys which I tried to fix, but more problems just keep appearing. I do understand that the DB is created with schema.rb file, but I'm sure it is just outdated by now. It is another issue I will maybe fix, but for now I just want to write some basic unit tests not even using the DB itself.
So the question is: is it possible to write just unit tests for some methods without invoking all the test DB scripts? I'm aware that this is maybe not the best practice, but I will feel better modifying the app with some test coverage and starting with fixing the DB I do not yet understand seems like a bad idea to me.
I'm using Ruby version 2.1.10 and the app is written in Rails 4.0.4 - these seem to be the latest versions I managed to run the app on.

How can I decrease my Rails test overhead?

I'm using Test::Unit on a large app with a large number of gem dependencies (>75). I'm trying to develop using BDD, but it takes minutes for the app to load it's dependencies before it can run the tests. Is there a way to preload the dependencies and just auto-run the test on changes, or a similar solution?
I would look into Spork. It works wonders.
https://github.com/sporkrb/spork
https://github.com/sporkrb/spork-testunit
I am using RSpec and there's a great tool for it, called Spork. It basically loads your app once and then just reloads modified parts. If you combine it with Guard, you get "continuous testing". That is, you hit 'Save' in your editor and tests start executing, giving you instant feedback. This still amazes me after some months :)
Edit
As #THEM points out, there's a plugin for Spork to support TestUnit. You should look into it.
There was also an interesting article about test speed on the 37Signals blog a while back. Might be of interest even if you end up going with Spork or another solution.

Rails execute script

I am building a script in on of my controllers to fill a database with an excel files data. I would build the function, then access it through a route. (That i guess i can protect with cancan) But i thought about it, and it doesn't seem very ... 'Railsy'.
I know the scripts folder exists, and it is probably for these kinds of tasks. I've tried googling stuff like 'rails execute script' and other stuff, but i can't find any good advice for what to do next.
I'm sorry if this seems kind of stupid, but in my apps i've been kind of hacking around stuff to make it work, so any advice on this task would be appreciated.
If you need to upload the file in the app and process it, it should probably go in the "lib"directory and be accessed like any other Ruby library/module/etc.
If it's something you need to run locally, "on demand", "scripts" is fine. If you need access to your rails environment when running it like any Rails models, you can run it from "rails console" or "rails runner".
As Aln said, there are a variety of ways it could be scheduled as well.
You could simply do
#!/usr/bin/env ruby
require 'rubygems'
# regular ruby code here
and have it running just like any other util. Of course you can always call any *.rb with simply
ruby somescript.rb
If you need some scheduled script, check into rufus-scheduler gem.

Autotest not running after changes to view

Rails 3.0.3, Rspec 2, Zentest gem - extremely frustrated right now, so give me some slack.
Why doesn't autotest rerun my integration (/request) and controller specs why I make changes to a view file? Am I missing something? It works fine when I make changes to my controller or spec file.
How do I make it do so?
Is there documentation that isn't like a swamp to wade through to figure out where the magic configuration incantations for autotest/zentest/etc are? It just seems like a big mess to me, so please tell me how dumb I am and point me in the right direction.
My understanding is that this is the default behavior when you run autotest with Rspec, that is, changes to view templates will only re-run the specs for the template.
Looking through the ZenTest docs won't help much, since the test framework (RSpec in this case) is where spec-file to view-file mapping actually occurs.
You can add new mappings in the .autotest file as described in this SO thread. I'd also take a look in the RSpec code itself to see all the mappings in one place, which can be found in {path_to_installed_gems}/rspec-rails-{version}/lib/autotest/rails_rspec2.rb

Why is Rake running a model for which I can find no test?

When I run "rake", it's loading up one of the models among all of the classes I have in my app/models directory. The thing is, it's not one I have a test for, it's just a model I have in there that is actually used with script/runner to run in the background and perform tasks for my main Rails application. At the end of the file I've got it creating a new instance of the class above and then running main for the class.
Since it loops indefinitely I definitely do not want it started up by the testing code. Why would the unit testing or Rake involve this other class in any way?
To my shame, I haven't been writing any tests for this code and I decided I would start writing some, but this stopped me right away because I can't even run Rake for what is out there now without it going haywire.
I'm not sure it's Rake's fault - I have a feeling that when you add :environment as a dependency, you're bringing up the whole Rails infrastructure, which may well involve requiring every model file (this is fairly wild guesswork - I haven't followed the boot process that deeply yet).
However it's happening, it seems that your model is being required, at which point all hell breaks loose.
Looking at script/runner and more usefully, railties/lb/commands/runner.rb, the execution sequence seems to be something like:
require 'boot' # boot the Rails app
eval(File.read(code_or_file)) # run what you asked for
That second line (it's actually around line 45 in runner.rb) looks like the key. How would it be if you defined a separate script (in /lib, say?) that contained the code that runs your model? I think that would probably be a more Rails-ish way to do it. And it would probably stop Rake messing up your tests...

Resources