Skip to content

Instantly share code, notes, and snippets.

@eliotsykes
Last active July 28, 2023 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliotsykes/af20ec63ed4f863dc81d3ef5c9add473 to your computer and use it in GitHub Desktop.
Save eliotsykes/af20ec63ed4f863dc81d3ef5c9add473 to your computer and use it in GitHub Desktop.
Testing Rails: defining temporary controller, view and route in feature spec
<% # This file lives at spec/features/views/index.html.erb %>
<h1>My Temporary Page</h1>
feature 'add temporary controller, view, and route for testing', :js do
before do
klass = Class.new(ApplicationController) do
prepend_view_path 'spec/features/views'
def index
# Put an index.html.erb file in spec/features/views
render template: 'index'
end
end
stub_const('TemporaryController', klass)
Rails.application.routes.disable_clear_and_finalize = true # preserve original routes
Rails.application.routes.draw do
get '/temp-path', to: 'temporary#index'
end
end
# Reset routes so they don't include test mapping from above.
after { Rails.application.reload_routes! }
scenario 'view temporary page' do
visit '/temp-path'
expect(page).to have_css 'h1', text: 'My Temporary Page'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment