简体   繁体   中英

Rspec testing of Rails gem

At my job we have a number of rails projects with similar functionality. We have refactored some of the common functionality out into a gem that is included in all the projects.

The gem is just another rails project, providing models, controllers, and views to be added to the projects that include it.

How do I test the gem? It's not clear to me how to test it independently, but it doesn't make sense to write tests of the gem in the projects that include it.

You should have tests in the gem's project itself.

As an example you can see that cancancan has a directory structure similar to all other rails projects, with a root level 'spec' folder.

You may have to use rspec rather than rspec-rails depending on your gem setup, but there isn't any real magic sauce here. You should write tests that cover the functionality of the gem itself, within the gem's codebase. But you shouldn't be writing tests for the gem in the projects that include it. Or at least not more than you might for other gems (so there may be some mocking / verification / interface building, but no direct testing of the gem).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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