getting rake test to see tests in a subdirectory..? - ruby-on-rails

I wanted to clean up my tests, so I broke a very large one up into multiple files and wanted to store them in a subdirectory inside test/integrations...
However, now rake test does not see them.. How can I tell rake to look in an additional place for test files?

I've had this issue too, and one thing you can do is place a test within the regular subdirectory (either unit, integeration, functional) and add the lines:
system("rake test TEST=test/integration/my_subfolder/test1.rb")
system("rake test TEST=test/integration/my_subfolder/test2.rb")
Etc. It's not the nicest solution, but it works. I can't remember now, but I don't think you can use regex in that singular TEST option.

Related

Clear Jasmine fixture after each test

I have a Rails 4 app and am using the jasmine but can't seem to find an example about how to run a global afterEach on my test suite. I want #jasmine_content emptied after each test is run so that there is no conflicts. I can add a afterEach call to each of my test files, but it would be nice to have it run for by default and never have to call it. Does anyone know how to do this? I was thinking you may be able to add a support file that always gets run, but haven't been able to do this yet.
Thanks
By default the jasmine gem will load any files in <spec_dir>/helpers before loading any of your tests. You can put a specHelper.js in there, similar to how you would for rspec, and just add an afterEach or beforeEach in there, but not in a describe and it will affect all specs in your suite.

rubymine only able to run model tests one by one

I am able to right-click on any of my 3 spec/models but when I right click the spec/models folder and select 'run all tests in models' I get 'Unable to attach test reporter to test framework'.
The first line of my model tests is: require 'spec_helper.rb'
I posted this answer for another question. It may help with this one as well:
On my system, the problem was the "redgreen" gem. It automatically colors the progress marks (dots) and the Test::Unit summary messages based on success or failure. There must be something in RubyMine that's trying to parse the Test::Unit results (the output of "rake test", I imagine), and it's choking on the ANSI sequences.
I commented out "require 'redgreen'" in my test/test_helper.rb, and the problem went away. I really like redgreen for executing "rake test" from the shell, though, so I put this in test_helper to make it work for both "rake test" and within RubyMine:
require 'redgreen' if $stdin.tty?
It may not be redgreen that's causing your problem, but be suspicious of anything which might create unconventional Test::Unit output.
Good luck!
I had the same issue and had to add the following to /etc/launcd.conf (I had to create this file as well):
setenv DYLD_LIBRARY_PATH /usr/local/mysql/lib/
and reboot. There are other ways to do it as well (a plist file, adding this environmental variable to RubyMine, etc… but this was most reliable. Of course, this assumes that you use MySQL.
The answer in the end for my setup was to fiddle around with the IDE settings including the ruby version, making sure it was 1.9.2 and the directories referenced in the config. screens were correct. This plus some restarts resolved the issue.

On Ruby on Rails, how to run ONE functional test when another one is breaking?

let's say you added a controller and action (example: story/index), and want to run a functional test by
rake test:functionals
and then you found that another part of the project your coworker is working on actually broke the test at an earlier place (another controller/action), before your functional test takes place.
In this case, can you run just one functional test, which is yours?
Wise-Ass Answer
If you coworker breaks the tests, then he should fix the test, or he shouldn't have committed the code to the repo. That is the main principle that we usually have in projects I work on.
Nice Answer
Try this
rake test:functionals TEST=test/functional/xy_test.rb
Or this
running one test without rake but with explicit $-loadpath works too
here "ruby -I directory" specifies the $loadpath. Otherwise you won't load test environment and "require test_helper" fails!
ruby -I test test/functional/xy_test.rb
1) Perhaps: rake test:units
2) This link might also help you out:
http://rake.rubyforge.org/
3) This might also help you out:
"Typical Rails tests come in the
follow forms:
Unit (Model) These test business logic
in your models. A well-written Rails
application should have the bulk of
its code in its models, so the bulk of
your tests should be these.
Functional (Controller) These test
individual controller actions in
isolation.
Integration (Controller to Controller)
These test state mutations
between/over multiple actions and
routing, i.e. ensuring that things
don’t totally explode as a user clicks
through a typical work flow.
Fixtures Used to hold example model
data used to easily instantiate those
models in tests, avoiding the tedious
process of manually creating model
objects.
Unit/Helpers These test helpers used
in views.
In other words, the basic
relationships look like this:
Model Unit Test Controller Functional
Test View (as part of a) Functional
Test Controller to
Controller Integration Test"
Found at http://rails-nutshell.labs.oreilly.com/ch09.html
Check out the single_test gem which makes it easy to run specific tests (or groups of tests)

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...

Ruby on Rails: Running Tests

When I want to run all my unit tests, I run rake test:units. To run all my functional tests, I run rake test:functionals. If I want to run all the test cases in one file, I run
ruby test/unit/username_test.rb
A few people have been telling me I should run rake instead such as
rake test:units TEST=test/unit/username_test.rb
For running tests, they say I should always run rake. I know I should run rake if I'm testing all my unit tests. But what if it's just one file or one particular test method in a file that I'm testing? Should I still use rake? Is there any difference between the two? Do I get any benefit from running rake over ruby? Is there any disadvantage to running ruby rather than rake?
Sadly, the two are not the same. Running the tests under rake can wind up pulling things from different places than when you run the test directly (more a problem when you have multiple versions of gems, etc. on your system).
The intent is that tests run under rake should be in an environment that matches what rails would produce; I can not attest to how closely they match, but I have seen tests that passed when run directly but failed when run via rake or rails (and visa versa).
Before checking in at the very least I'd recommend running rake to hit everything, in order to be assured that nothing unexpected has broken.
Plain ruby seems ideal for fast testing of single files during iterations.
Be aware that running everything through rake can produce different results to running everything individually, as I found to my confusion recently - I was doing something slightly wrong in one test that worked successfully in isolation but that left a problem lying around for a subsequent test that only showed up when I used rake.
No I dont think so. Rake seems to be a convenient way to run all tests, all unit tests or all functional/controller tests.
For a single file, I use the ruby object_test.rb approach.. shorter and works fine for my rails home project.
They should be identical. if they are not, you're definitely doing something wrong.
As I said in my other comments, if you get tests that pass in one, but fail in the other, you're doing something very wrong; this indicates a poor test setup, and is usually caused by a different test run order between the two test approaches; one of which causes tests to fail.
Usually the cause of this is that you are not using transactions and/or tests are not cleaning up after themselves. For example, not properly requiring the fixtures they later test for, and instead relying on the pre-existing database state.
you are free to use either method. if something breaks, you're doing something wrong in your tests, and you should fix your code.
The two are not the same. Rake will do some preliminary test loading.
The intent is that tests run under rake should be in an environment that matches what rails would produce;
One difference I've noticed is with rake some of the fixture loading happens which could be by-passed with ruby.
I'd recommend using rake, unless you are using the ruby command line to one just one test in the file with the -n option.

Resources