RSpec/Gem development: Stubbing rails templates - ruby-on-rails

I'm currently working on a couple of different gems both of which mainly consist of rails view helpers.
My problem is that some of these helpers require rendered rails templates to test the output - but I am unsure of the best way to stub a template, without a rails application. I presume that I should be able to leverage rspec-rails in some capacity, but i've been having trouble getting that to work without a rails app present.
Am I approaching this the wrong way? What's the current best-practice to test rails-specific features (in particular - things that happen only in the view) during gem development?

I use the excellent enginex gem, which helps you in setting up a gem skeleton with an embedded rails application for testing. This way you can easily test any rails dependency inside that application. To generate rspec tests run it as follows (default is test-unit):
enginex -t rspec your-gem-name
What I did to incorporate this into my gem, was run this inside some test folder, and copied the necessary files over to my gem. As an example, you could check it out inside my cocoon gem.
Hope this helps.

Related

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).

How to get other Gem helpers into my Engine Gem's tests?

I'm trying to write tests for an engine gem I'm writing. I'm using Rspec and the tests seem to be running fine. But whenever a view uses a helper from another gem, such as "will_paginate" or "ransack", I get an "undefined method" error.
I've tried including the other gems in my gem's Gemfile (in addition to the engine.gemspec file) as well as the dummy app's Gemfile, but I get the same error. I've also tried including the gems in the spec/spec_helper.rb file.
So I've tried most of the things mentioned here:
Setup RSpec to test a gem (not Rails)
Usually, for Rspec tests for a regular Rails app, these helpers seem to be just included some how since I don't have this issue running tests for a regular Rails app.
I also have been needing to namespace my url helpers in the views with something like:
engine.resources_path
I'm not sure if that's a symptom of some configuration I've messed up on.
Everything in the engine runs fine when mounted to another app and viewed on the browser.
Any ideas?
Turns out a better approach is to stub out methods from gems since the gem should be testing their own methods anyways. Please let me know if I'm misunderstanding anything. Thanks!

How to correctly embed a test Rails application inside the gem that it tests?

I have a small gem that I have created and a separate small Rails application that I use to test the gem.
I have been looking for information about how I could embed the test rails app inside the gem so it's packaged as one and so I can launch it to test the application.
I have currently got it inside the gem tree in a subdirectory of test. I can change to that subdirectory and run the app up ok, or I can run its tests with rake. But is this the correct way to do this ?
(in case it makes any difference, this gem and the small test app are for Rails 2.x)
I'd suggest you take a look at some other gems on github and what they do. Some probably don't need a full rails app, but devise, for instance has a barebones rails app embedded at /test/rails_app
https://github.com/plataformatec/devise/tree/master/test/rails_app

regenerating rspec files

how can I generate rspec on-demand?
the thing was my rspec files were already automatically generated by the "rails generate controller" command. Then I manually deleted those files in hope that there should be a command which I can use to regenerate those files.
What do I do to regenerate those deleted rspec files without firing "rails generate controller" again?
I have tried some command I was suggested by some blog:
$ rails generate rspec_controller pages --skip-migration --skip-fixture --skip
Could not find generator rspec_controller.
but it didn't work for me.
any advice would be really appreciated!
In the root folder of your app:
rails generate controller -h
It will show you the usage instructions.
rspec_controller is deprecated in favour of the standard controller generator in Rails. It works now by you including rspec-rails within your Gemfile like this:
group :development, :test do
gem 'rspec-rails'
end
This then will load this file, which customizes what tools the Rails controller generator uses with these two lines.
This is a bad idea, for two reasons:
You normally shouldn't be autogenerating your spec files: you need to think about exactly what you want to specify.
Controller specs should normally not be written at all. Use Cucumber scenarios instead -- they're much less painful and much better at testing behavior (controller specs are too dependent on implementation).

Reference checklist for starting a new Rails application?

It's easy to create a new Rails application using "rails new myapp" (or start with a template from Rails Wizard or the RailsApps project).
But after that, what do you do? What gems do you always add? What decisions do you make before beginning development? What important items are often overlooked before deploying an app?
I'd like to put together a reference checklist to share. Any suggestions?
For example:
Create a new RVM gemset
Modify the .gitignore file
Switch from TestUnit to RSpec
Set up Guard to run tests in the background during development
Add the viewport metatag to the default application layout
Don't forget to create a sitemap.xml file
Add a Google Analytics snippet
What else?
Starting with a Rails template.
You should look theses resources :
http://railswizard.org/
https://github.com/RailsApps/rails3-application-templates
http://railsapps.github.com/rails-application-templates.html
https://github.com/quickleft/prologue
For me usual process involves:
Add CSS framework (grids, text, forms)
Add Cells
Add Slim (www.slim-lang.com)
Remove Test::Unit for RSpec
Add application config settings (config.yml)
Add Cucumber
Add FactoryGirl
Add Spork
Add Guard (guard-rspec, guard-cucumber, guard-sass, guard-livereload, guard-spork)
Add Git, Github space, + amend .gitignore
Add Heroku (stage + production) spaces
I'll usually copy over my google_analytics helpers and sitemap_controller from other projects during the development process instead of being organised enough to do it from the start. I like to the the testing and deployment options setup from the get go so I can start developing and releasing early and often.
Dave
create rvm gemset, create .rvmrc, modify .gitignore
Then add gems
gem 'pg'
gem 'thin'
gem 'ruby-debug19', :require => 'ruby-debug'
gem 'rspec-rails'
gem 'factory_girl_rails'
gem 'capybara'
then depending on the project, I often use aws3, paperclip, resque, will_paginate and haml (although I try not to use it on new projects anymore)
Most of the time:
Configuration
add .rvmrc
amberbit-config gem (avaible at GH)
modify .gitignore
Views
haml to sass/coffee stack
rdiscount
Tests
rspec instead of unit tests
capybara, factory_gril, spork, timecop
Development
guard-livereload, with guard, libnotify etc.
active_reload for faster development with assets pipeline
annotate if relational db
pry
I almost forgot to mention: mix of html5 boilerplate for rails with twitter bootstrap it's good combo.
The first think that I do is head to http://railswizard.org/ and create a template, before "rails new app".
I always want to set up Factory Girl under /fixtures, and setup
Cucumber along Rspec. Sometimes I use shoulda too.
Initialize the project as a git repository and link it to
github. Set up the app to use PostgreSQL instead of SQLite.
And last I can think of is that I often make an entry, from the
beginning, to load .rb files form /lib automatically.
I don't add anything. Things get added if project requires them.
I don't load up CSS framework just because there's a need for two columns and a rounded button somewhere.
I don't load FactoryGirl because rails test fixtures actually do a fine job as long as you know how to use them. (Hint: you don't need 100 instances of User in your fixtures)
I don't load RSpec/Cucumber/etc because UnitTest is just as good and I prefer keeping things simple.
There's absolutely no reason to bloat project with things just because you "might need it"
I got tired of having to remember and repeat the mundane tasks required for every new app.
If you're looking for guidance on getting started, we've recently (yesterday!) released a tool to speed up the process, with all sorts of tried and tested libraries for the various aspects of a Rails web app, as well as cleanup scripts to get the fresh app just the way it should be.
Check out http://drone.bz to build an app the same way we do. Under the hood, it uses the app_drone gem to resolve dependencies and build the actual template, but the UI is probably all you need to get started.
There are several similar tools out there, but I decided to be highly opinionated in my recipe choices, and the end result is a solid foundation on which to start developing :)
P.S. Even if you don't use the drones, you can take a look at the steps that are common enough in Rails dev to be automated.

Resources