Skip to content

Instantly share code, notes, and snippets.

@asonas
Created January 6, 2022 08:57
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 asonas/bf7b7c98d7701c6ad89d178c0aa2347e to your computer and use it in GitHub Desktop.
Save asonas/bf7b7c98d7701c6ad89d178c0aa2347e to your computer and use it in GitHub Desktop.
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "rails", "7.0.0"
gem "database_rewinder", "0.9.6"
gem "rspec-rails", "5.0.2"
#gem "mysql2", "0.5.3"
#gem "ridgepole", "1.0.0"
gem "sqlite3"
gem "pry"
end
ENV['RAILS_ENV'] ||= 'test'
require "action_controller/railtie"
require "active_record"
module App
class Engine < ::Rails::Engine
isolate_namespace App
end
class UsersController < ActionController::Base
def create
User.create
end
def destroy
User.first.destroy
end
end
config = ActiveRecord::Base.configurations = { "test": { adapter: "sqlite3", database: ":memory:" } }
ActiveRecord::Base.establish_connection(config["test"])
ActiveRecord::Schema.define do
create_table :users
end
class User < ActiveRecord::Base
end
end
App::Engine.routes.draw do
post "/users" => "users#create"
delete "/users" => "users#destroy"
end
Rails.application.routes.draw do
mount App::Engine, at: "/test"
end
require "rspec/rails"
require "rspec/autorun"
require "database_rewinder/railtie"
require "database_rewinder/cleaner"
module App
RSpec.configure do |config|
config.before(:suite) do
DatabaseRewinder.init
DatabaseRewinder.clean_all
end
config.after(:each) do
DatabaseRewinder.clean
end
end
RSpec.describe "RequestUser", type: :request do
describe "POST #create" do
it "should create record" do
post "/users"
expect(User.count).to eql 1
end
end
describe "POST #create" do
it "should destroy record" do
delete "/users"
expect(User.count).to eql 0
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment