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:
- You can use regular
/ims/users/new
andcreate
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 tocreate
- Use dynamic constraints on routes (this way routes will still be listed in production/test, but not accessible)
- Just leave the routes as always present and raise an error in controller for wrong environments
- 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
Typeerror: Object Doesn't Support This Property or Method
Ruby on Rails Active Admin Has_Many Changing Dropdown to Use a Different Column
How to Make a Ruby Script Using Trollop for Command Line Parsing
Compass Error for Command 'Grunt Server'
Capybara Trouble Filling in Js Modal
Marshal Ruby Hash with Default Proc - Remove the Default Proc
Using $1, $2, etc. Global Variables Inside Method Definition
Floating Point Precision in Ruby on Rails Model Validations
Ruby - Return Byte Array Containing Two's Complement Representation of Bignum/Fixnum
Building Gem, Executable Not Found
Using Passphrase Callback in Ruby Gpgme
Why Is 032 Different Than 32 in Ruby
How Is Ruby Tcpsocket Timeout Defined