how would I go about 'starting over from scratch' with Rails tests? - ruby-on-rails

I have an existing Rails app that I built using Rails 3, Mongoid/Mongodb and Devise. The app is running fine. I'd now like to add some tests to it (sure, shoulda done this in the beginning but the learning curve for just Rails was enough...).
I've used several pages to get it going, especially the Rails guide and this blog post about Mongo and Cucumber/Rspec. My concern here is that between all of the "add this to this and such file" that I've done to try and get this working (and it's not) I've made such a mess of things that it might be better to start over from scratch. With the testing portion of the app.
I thought I would just delete the spec and test directories and re-gen the tests but I can't find a command to do that (the regen).
I've built a very simple test (assert true) but I'm getting:
D:/Dev/TheApp/test/test_helper.rb:10:in `<class:TestCase>':
undefined method `fixtures' for ActiveSupport::TestCase:Class (NoMethodError)
I think the real issue here is that I'm using MongoDb and the test architecture in Rails seems to really really want to do ActiveRecord. Not sure if those two are compatible.
Is there a quick way to build a barebones test directory? My short term solution is to just roll back those directories. Hoping for a better solution.

The blank tests are really worthless. If you didn't have tests/specs of value, then just start from scratch. And if you want to start over, you should just delete them and start new.
You could treat your code as "legacy code" as defined by Michael Feathers in Working Effectively with Legacy Code -- that is, code without tests.

Take a look at this getting started with rails testing guide over at 10gen:
http://www.mongodb.org/display/DOCS/Rails+-+Getting+Started#Rails-GettingStarted-Testing

Related

How to convert rails app to test files?

I like to create rspec test from working rails 5 app.
It' could be template to work on.
For example:
-scaffold will create test files template which is nice.
-Or simplecov to help increase test ratio (if I understand correctly)
-So if any tools that could run through each line of controller method and re-create each expected/result put in the test file.
Are there any gem or solution ?
Why ask this question?.
I understand this is not a purpose of test. However test is to save time in future and now as well. Many rails app don't have test, and to go back each line of running code will cost again, assuming the app is good at this stage. If we can have all test at this point and use it to control / run for future development that would be good
I found another gem that answer my question
rspec-kickstarter
But to use with Rails 5 it need to edit the path that created.

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.

Run controller test on two different Rails applications

I'm helping to migrate a Rails 2 app to Rails 4, and I have come in towards the end of the project. The Rails 2 app has no tests.
Obviously we need tests to make sure the Rails 4 app works, but we don't want to write the tests in Rails 2 syntax, and then go through the pain of migrating the tests to Rails 4. We currently use RSpec on other projects.
I thought of writing them in Rails 4 as feature tests, and using Capybara.app_host to re-route the requests to a running Rails 2 application instance. Problem is, that the application is an API, and Capybara is not meant for testing APIs.
Is there any way of routing RSpec controller tests to another server instance?
Thanks again for the comments.
I think we have a plan. We tried to get cucumber and cucumber-api-steps running on our old project, but we realised that it was going to be hard work getting old gem versions to work nicely together.
The solution I'm trying now is using RSpec 1.3 and creating a spec/requests folder and adding controller tests to it.
I got this idea from a post from http://matthewlehner.net/rails-api-testing-guidelines/ - thanks Matthew. I don't see request specs in the docs until RSpec 2, so we make our controller tests pretty close to integration tests using integrate_views, then check the contents of the body returned by the controller.
As for the syntax, maybe we can include an expect(..) helper to return an object that calls #should from a #to method in the Rails 2 project and remove it when we port the Rails 4 project.

Should Cucumber use the RSpec features folder?

First off, I understand this is a total newb question, but I'm just a little confused about this one thing:
When I installed RSpec, a folder was created in ./spec/features. Then when I installed cucumber, a folder was created in ./features. I was wondering if I should configure cucumber to use the rspec features folder and if so how? Or should I delete the rspec features folder? Or am I not clear about something and each folder is used for something different?
I realize this is probably a matter of opinion, but I'm just wondering what the popular best practice is. Any tips?
You can simply delete the rspec features folder. You won't need it if you use rspec for testing your models and controllers, and cucumber for your integration tests

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.

Resources