당신을 같은 내 테스트 세계에서 가장 간단한 랙 앱이 필요합니다.
또한 미들웨어의 생성자는 첫 번째 매개 변수로 앱을 받아야합니다. TER 해시 읽어야하지 않도록 모든 가능성에
subject { MyMiddleWare.new(app) }
을하지만, 테스트는 미들웨어가 요청에 미친 어떤 영향을 결정해야 할 것이다. 따라서 미들웨어를 감시하기 위해 좀 더 정교한 랙 응용 프로그램을 작성할 수 있습니다.
class MockRackApp
attr_reader :request_body
def initialize
@request_headers = {}
end
def call(env)
@env = env
@request_body = env['rack.input'].read
[200, {'Content-Type' => 'text/plain'}, ['OK']]
end
def [](key)
@env[key]
end
end
그런 다음 실제로 요청을 보내려면 Rack :: MockRequest를 사용하는 것이 좋습니다. 예 :
describe MyMiddleWare do
let(:app) { MockRackApp.new }
subject { described_class.new(app) }
context "when called with a POST request" do
let(:request) { Rack::MockRequest.new(subject) }
before(:each) do
request.post("/some/path", input: post_data, 'CONTENT_TYPE' => 'text/plain')
end
context "with some particular data" do
let(:post_data) { "String or IO post data" }
it "passes the request through unchanged" do
expect(app['CONTENT_TYPE']).to eq('text/plain')
expect(app['CONTENT_LENGTH'].to_i).to eq(post_data.length)
expect(app.request_body).to eq(post_data)
end
end
end
end
여기에 제목이 필요하지 않습니다. 실제로 어떻게 작동합니까? – Calin
이것은 실제로 유효한 랙 앱이 아닙니다. 'lambda '는 유효한 Rack 앱이되기 위해 논쟁을해야 할 것입니다. – branch14
제목에 게시하도록 업데이트되었습니다. - 감사합니다, @ Calin. – Ritchie