Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 972
Sebastian Avatar asked Sep 14 '16 10:09

Sebastian


2 Answers

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.

like image 91
Stewart Avatar answered Sep 20 '22 02:09

Stewart


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

like image 23
dgmora Avatar answered Sep 22 '22 02:09

dgmora