How to cleaning document.body after each test globally? - ruby-on-rails

I am using Jasmine gem in my Rails project for testing JavaScript. My tests add some HTML to document.body. I want it to be clean before each test. Is there a way to clean it globally for all tests in all test files? I do not want to put the cleaning in beforeEach in each suite.

Yes you can here are two ways;)
Put the beforeEach on your runner. Here's the docs with concrete example.
You can use jasmine-jquery's fixtures or see how it's done there if you don't need jQuery. With it, whenever you create a fixture it's automatically cleaned up after the test.

Related

How to test generators with RSpec

Is there a recommended way of testing Rails generators with RSpec?
The only solution I've found is the Generator Spec gem, that hasn't been updated in over two years.
There's the gem
https://github.com/petergoldstein/generator_spec, which does a decent job, although it is not very actively maintained
I would write a file by hand that acts as a test fixture. I would then as part of my test generate the file with the generator. At that point I would diff the two files. Looks like the diffy gem could help you there. If there is no diff then pass the test. Fail if otherwise.
https://github.com/samg/diffy
Don't forget to clean up your temp files after the tests. You don't want them hanging around in your repos.
I didn't found official recommendations how to test Rails generators too. So, I am just run generator directly on dummy application, compare generated files and delete them at the end of test.
Here is my spec which implement described approach. I had used minispec instead of rspec here but they a very similar.

How do I do fast unit testing in a Rails app without loading the Rails environment?

In a Rails app, I would like to have a setup that allows me to run fast unit tests of Rails independent application logic.
Have an alternative minimalistic test_helper.rb file where you just load minitest and make sure to require the code under test in your testcase file. That way you can run the test in isolation quickly and you can also run the test via Rake with the Rails environment loaded. See this gist for a code example.
There is explanation article about spec_helper.rb for rspec. You can use it analog for unit tests, of course.

Why won't my features specs run in RSpec?

In my Rails 4 app, I'm using Rspec for testing. My directory structure is
spec
-- controllers
-- factories
-- features
-- spec_helper.rb
-- support
When I run rspec spec, it runs my tests in controllers, but not in features. I can specify rspec spec/features and they'll run, but I want to be able to run all tests under spec at once. My guess is it's not looking in features because of a configuration setting, but I don't know where this is.
I've tried different incantations of starting rspec but haven't had any luck.
Based on your feedback to the comments above, the issue is one of file naming. I've definitely been burned by that before too. By default Rspec will go through the files looking for ones ending with _spec.rb, this default behaviour is overridden if you specify the folder manually.

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.

getting rake test to see tests in a subdirectory..?

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.

Resources