How to test generators with RSpec
Asked Answered
T

3

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.

Twodimensional answered 14/9, 2016 at 10:6 Comment(0)
S
3

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

Selfgovernment answered 18/5, 2017 at 8:21 Comment(2)
A link to a solution is welcome, but please ensure your answer is useful without it: add context around the link 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.Trigonous
Thank your for suggesting that gem, it works great!Tannenbaum
Q
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.

Qoph answered 14/9, 2016 at 10:9 Comment(0)
D
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.

Doerr answered 14/9, 2016 at 10:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.