@kiwa-lab/ruby
v2.1.0
Published
Ruby framework mock harness for kiwa — Rails / Sinatra / Roda / Hanami の request → controller → response cycle を統一 interface で in-process invoke する test infra。 ActiveRecord query 捕捉 + ERB render + session/cookies snapshot を real Ruby runtime なしで叩ける。
Maintainers
Readme
@kiwa-lab/ruby
Ruby framework mock harness for kiwa — Rails / Sinatra / Roda / Hanami の request → controller → response cycle を TypeScript から in-process で叩く test infra。 real Ruby runtime 不要。
Installation
pnpm add -D @kiwa-lab/ruby
# or
npm install -D @kiwa-lab/ruby
# or
yarn add -D @kiwa-lab/rubySupported frameworks
| Framework | Dispatch style | Status | |---|---|---| | Rails | ActionController (before_action / render / redirect_to) | ✅ | | Sinatra | generic route | ✅ | | Roda | generic route | ✅ | | Hanami | generic route | ✅ |
Quick start
import {
createRubyAppEnv,
dispatchRailsRequest,
dispatchGenericRequest,
renderERB,
captureActiveRecord,
} from '@kiwa-lab/ruby';
const env = createRubyAppEnv({ framework: 'rails' });
const result = await dispatchRailsRequest(env, {
controller: 'UsersController',
action: 'show',
request: { method: 'GET', path: '/users/1' },
handler: async (ctx) => {
ctx.render({ status: 200, template: 'users/show', locals: { name: 'kiwa' } });
},
});
// result = { status: 200, renderCalls: [...], redirectSignals: [...] }
const rendered = renderERB('<h1><%= name %></h1>', { name: 'kiwa' });
const ar = captureActiveRecord(env);
env.record({ op: 'select', sql: 'SELECT * FROM users', bindings: [] });API reference
createRubyAppEnv(options: CreateRubyAppEnvOptions): RubyAppEnv— Rails / Sinatra / Roda / Hanami mock env 生成RubyAppEnv.route(method, path, handler): void— generic route 登録dispatchRailsRequest(env, options): Promise<RailsDispatchResult>— Rails controller 経路 dispatchdispatchGenericRequest(env, req): Promise<GenericDispatchResult>— Sinatra / Roda / Hanami 経路renderERB(template: string, locals: ERBLocals): ERBRenderResult— ERB<%= %>interpolationcaptureActiveRecord(env): ActiveRecordSnapshot— ActiveRecord query log snapshot
Test integration
import { describe, expect, it } from 'vitest';
import { createRubyAppEnv, dispatchRailsRequest } from '@kiwa-lab/ruby';
describe('users#show', () => {
it('200 で render される', async () => {
const env = createRubyAppEnv({ framework: 'rails' });
const r = await dispatchRailsRequest(env, {
controller: 'UsersController', action: 'show',
request: { method: 'GET', path: '/users/1' },
handler: async (ctx) => { ctx.render({ status: 200, template: 'users/show' }); },
});
expect(r.status).toBe(200);
});
});/kiwa-ruby skill を起動すると Rails controller + ERB + ActiveRecord 3 経路の test を生成できる。
License
UNLICENSED — see cardene777/kiwa for repo terms.
