Test (With Rspec) a Controller Outside of a Rails Environment

Testing environment dependent routing with RSPEC

Routes are loaded once on app start, and Rails.env will be 'test'.

Usually it's better to keep you development environment as close to production as possible, including routes.

If you want some shortcuts in dev env, there're options:

  1. You can use regular /ims/users/new and create actions with a parameter that will only have effect in development. For example /ims/users/new?development=true that will also render additional hidden field to pass state to create
  2. Use dynamic constraints on routes (this way routes will still be listed in production/test, but not accessible)
  3. Just leave the routes as always present and raise an error in controller for wrong environments
  4. Extract whole development_account-feature into an engine, test separately and mount it only in development (the hardest way, not always possible)

I'd go with first option, it's both simple, incapsulates relatively well and most probably your *_development_account mimic corresponding actions anyway

framework integration testing within a gem: how to set up rspec controller test for a gem rails integration

Below is a nice minimal example of a rails controller integration test from a gem.
my_gem. Assume that you are in a gem root with simple rspec setup (say with rspec --init). Then spec/rails_controller_integration_spec.rb will look like this.

rspec/rails does the necessary requires, among them rspec/rails/example which sets up example group types based on metatags. The metatag :type => :controller drives the inclusion of the proper group module RSpec::Rails::ControllerExampleGroup which provides you with all the goodies of rails controller specing as well as all the goodies of ActionController::TestCase like get/post.

Hope this helps.

What I still don't get is how the Rails environment gets assigned. In particular what if I would like to set up two apps TestTailsApp1 and TestTailsApp2. any advice?

require 'spec_helper'
require 'my_gem'

require 'rails'
require 'action_controller/railtie' # allows ActionController::Base
# crucial part here:
require 'rspec/rails'
# note that require 'rspec-rails' does not work

module TestRailsApp
class Application < Rails::Application
# app config here
# config.secret_token = '572c86f5ede338bd8aba8dae0fd3a326aabababc98d1e6ce34b9f5'
# routes.draw do
# resources :models
# end
end

class ApplicationController < ActionController::Base
# setup
end

end

describe 'My gem' do

context "in a Rails controller", :type => :controller do

controller(TestRailsApp::ApplicationController) do
extend(RSpec::Rails::ControllerExampleGroup::BypassRescue)
# example-specific setup for anonymous controller
# https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs/anonymous-controller
def index
end
end

before(:each) do
# request needs to be setup to avoid path setting error
@request = ActionController::TestRequest.new
end

describe "#index" do
it "works" do
get :index
response.body.should == 'index content'
end
end
end

end

Tell rspec (core) that test is testing controller

Note: I'm not using rspec-rails.

That's your problem right there. All the rails type specs (controller, request, features, views) are part of rspec-rails not rspec-core.

Without rspec-rails the type metadata does absolutely nothing - its just a plain example group describing a class.

The solution is to add rspec-rails to your gemfile.

group :development, :test do
gem 'rspec-rails', '~> 3.6'
end

And run rails g rspec install.

  • https://github.com/rspec/rspec-rails


Related Topics



Leave a reply



Submit