简体   繁体   English

如何使用RSpec测试生成器

[英]How to test generators with RSpec

Is there a recommended way of testing Rails generators with RSpec? 有推荐的方法用RSpec测试Rails生成器吗? The only solution I've found is the Generator Spec gem, that hasn't been updated in over two years. 我发现的唯一解决方案是Generator Spec gem,两年内还没有更新。

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 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. 我没有找到如何测试Rails生成器的官方建议。 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. 我在这里使用了minispec而不是rspec ,但它们非常相似。

宝石 https://github.com/petergoldstein/generator_spec ,它做得不错,虽然不是很积极地维护

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM