How to test generators with RSpec - ruby-on-rails

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.

Related

How to generate cucumber-rails for a different folder

I'm trying to create a test folder for my rails project using cucumber-rails generator (the gem is here: https://github.com/cucumber/cucumber-rails ).
The problem is that when I run "rails generate cucumber:install" it creates a folder in my project named "features" with the cucumber files inside that folder, which is - in my perspective - not very organized.
I would like all the cucumber files to be inside a folder named "test". Is there a way that allows me to do that? I tried to run the generator inside the "test" folder but it didn't seem to work.
Anyone knows how to do this? Thanks a lot!
There are essentially 3 test libraries in Ruby and corresponding common assumptions:
test is expected to contain Test::Unit or MiniTest tests
spec is expected to contain RSpec tests
features is expected to contain cucumber tests
You should follow these assumptions or you may end up having several troubles in the long run.
In any case, to answer your question, what you want to do is not possible. The features folder is hard-code in the generator and in the cucumber task file. Therefore, unless you want to introduce several hacks and workaround, you'll have to follow the conventions.
I think there is a pretty straight forward solution for this:
In an issue it is pointed out that we can point to different paths for our features in the cucumber.yml
default: features
// becomes
default: test/features
But, then it can't find the step_defintions if you also add your custom path as a required flag then it can pick up the step_defintions as well.
default: features
// becomes
default: test/features -r test/features/step_definitions
if you don't want to use cucumber.yml you can use the cucumber cli to take care of it:
bundle exec cucumber test/features -r test/features/step_definitions
NOTE: I am using this in a docker environment and this works fine for both local and docker implementations.

RSpec "retest broken tests from last time" flag

Is there any way to retest previously broken tests?
So, say, I run rspec and several tests in different files and directories fail.
I fix something and now I have to manually specify all files and folders I want to retest or run tests for whole project again(It takes considerable amount of time for big projets).
What I was looking for is something like a flag
rspec --prev-failed-only
I realize that such flag would require considerable amount of additional actions from rspec, like storing results of previous tests and so on. But I think it would be super convenient to me.
Is there any such(or similar) tool/gem?
rspec-rerun gem does what you want : https://github.com/dblock/rspec-rerun
https://github.com/rspec/rspec-core/issues/456 has a good discussion on the topic of making rspec itself be able to rerun failed tests.
On the Giant Robots podcast, Sam Phippen of the core team mentioned this feature is due to be added to RSpec soon.
In case anyone else finds this, a month after the question was asked (open source <3 ) rspec 3.3 introduced the --only-failures option, also the delightfully handy --next-failure (-n) option. rspec --help for more info.

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

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.

What Test Environment Setup do Committers Use in the Ruby Community?

Today I am going to get as far as I can setting up my testing environment and workflow. I'm looking for practical advice on how to setup the test environment from you guys who are very passionate and versed in Ruby Testing.
By the end of the day (6am PST?) I would like to be able to:
Type one 1-command to run test suites for ANY project I find on Github.
Run autotest for ANY Github project so I can fork and make TESTABLE contributions.
Build gems from the ground up with Autotest and Shoulda.
For one reason or another, I hardly ever run tests for projects I clone from Github. The major reason is because unless they're using RSpec and have a Rake task to run the tests, I don't see the common pattern behind it all.
I have built 3 or 4 gems writing tests with RSpec, and while I find the DSL fun, it's less than ideal because it just adds another layer/language of methods I have to learn and remember. So I'm going with Shoulda. But this isn't a question about which testing framework to choose.
So the questions are:
What is your, the SO reader and Github project committer, test environment setup using autotest so that whenever you git clone a gem, you can run the tests and autotest-develop them if desired?
What are the guys who are writing the Paperclip Tests and Authlogic Tests doing? What is their setup?
Thanks for the insight. There are tons of resources describing how to use the different testing frameworks, but almost nothing on the actual setup and workflow. Looking for answers that will make me a more effective tester.
The most common convention probably is rake test, rake spec, or maybe even just rake.
Of course, there is no question that this will fail with many projects, in particular the ones without tests or specs.
It might be possible to parse the output of rake -T if a Rakefile is there, and act on that, but there really is no way you will cover ALL projects on GitHub.

Resources