Last active
July 28, 2023 16:29
-
-
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 contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% # This file lives at spec/features/views/index.html.erb %> | |
<h1>My Temporary Page</h1> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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