9

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.

Sebastian
  • 2,154
  • 1
  • 26
  • 42

3 Answers3

3

There's the gem https://github.com/petergoldstein/generator_spec, which does a decent job, although it is not very actively maintained

dgmora
  • 1,239
  • 11
  • 27
  • A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted.](//stackoverflow.com/help/deleted-answers) – Petter Friberg May 18 '17 at 08:22
  • Thank your for suggesting that gem, it works great! – ZedTuX Nov 18 '20 at 07:08
2

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.

Stewart
  • 3,023
  • 2
  • 24
  • 40
1

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.

Maxim
  • 9,701
  • 5
  • 60
  • 108