How to convert rails app to test files? - ruby-on-rails

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.

Related

Is there a way to generate a test folder?

I'm pretty new to rails and I created a rails application and deleted my test folder to save space. Now that I want to learn TDD, I want to know if there is a way to generate the test folders. To put simply, a reverse of -T.
rails new [name] -T
Run rails g ... in another folder, and move the test folder over. There's very little in the default tests, so there's no point in making a custom generator for them.
BTW congratulations on deciding to TDD. Minutes of TDD will save you hours of debugging.

How to generate a dummy app inside an already existing rails engine

I came to work for a company recently that has been working on a specific rails project for a while. Dropped in the middle of the development process, I'm beginning to go back and write tests for the existing code as well as the code currently being produced.
Testing the rails 4 app was easy enough, but once I got to testing the engine, I hit a mental block. After doing my due diligence, I found that most people take an approach like this (http://viget.com/extend/rails-engine-testing-with-rspec-capybara-and-factorygirl), but the common thread I saw in all of these responses was that people were building these apps from scratch, and thus had a dummy app generated for them automatically. I don't have the benefit of being able to generate everything from scratch, and have to work with what I was given, so what
I'd like to know is if there's a way to retroactively generate just the dummy application. Is there something simple I can type into the console and have it generated for me? Or is there a longer, slightly less pretty route? Or would a different strategy be better altogether?
Given that the dummy app is not supposed to be tied to the parent gem except for some vague names, that may do it:
cd some_path_where_your_engine_IS_NOT
rails plugin new YOUR_ENGINE_NAME --mountable --dummy-path=spec/dummy --skip-test-unit
mv YOUR_ENGINE_NAME/spec/dummy /real/path/to/YOUR_ENGINE_NAME/spec
rm -rf YOUR_ENGINE_NAME # cleanup useless cruft
Also, you may be interested in this answer to generate the app with the exact same rails version.
If the engine is within a parent app delete the engine from your Gemfile
Rerun the rails plugin new .. command you used to create the rails engine, but with the --skip parameter. This will create all the files, including dummy that do not exist in your engine, but leave existing files alone.

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.

how would I go about 'starting over from scratch' with Rails tests?

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

How can I run Ruby specs and/or tests in MacVim without locking up MacVim?

About 6 months ago I switched from TextMate to MacVim for all of my development work, which primarily consists of coding in Ruby, Ruby on Rails and JavaScript.
With TextMate, whenever I needed to run a spec or a test, I could just command+R on the test or spec file and another window would open and the results would be displayed with the 'pretty' format applied. If the spec or test was a lengthy one, I could just continue working with the codebase since the test/spec was running in a separate process/window. After the test ran, I could click through the results directly to the corresponding line in the spec file.
Tim Pope's excellent rails.vim plugin comes very close to emulating this behavior within the MacVim environment. Running :Rake when the current buffer is a test or spec runs the file then splits the buffer to display the results. You can navigate through the results and key through to the corresponding spot in the file.
The problem with the rails.vim approach is that it locks up the MacVim window while the test runs. This can be an issue with big apps that might have a lot of setup/teardown built into the tests. Also, the visual red/green html results that TextMate displays (via --format pretty, I'm assuming) is a bit easier to scan than the split window.
This guy came close about 18 mos ago: http://cassiomarques.wordpress.com/2009/01/09/running-rspec-files-from-vim-showing-the-results-in-firefox/ The script he has worked with a bit of hacking, but the tests still ran within MacVim and locked up the current window.
Any ideas on how to fully replicate the TextMate behavior described above in MacVim?
Thanks!
There is a plugin called vim-addon-background-cmd that can allow you to run tasks in the background instead of locking up the vim interface. You would have to create the call to run through the background command. See the docs for more information on how to do that.
A few months back I was looking for this same exact thing. Then I discovered autotest with rspec. Now I keep a separate terminal window open which shows my last run tests. If I change any relavent code files my tests are automatically run for me (the files are watched and if they change the tests run).
If you want the same autotest type behavior in a non-rails project you can look at the watchr gem. It's functionality is similar to autotest but you can use it in ANY framework.

Resources