Skip to content

How do I integrate it with Sinatra? #204

@Nakilon

Description

@Nakilon

I'm not a web developer so I never used Capybara integrated with the Ruby web app -- I always ran the tests as a separetly hosted application. However I'm currently making a Sinatra app and I suppose integrating it with the minitest would allow me to start and stop the app automatically that would be nice. But I can't figure out how to do that. I tried to follow https://sinatrarb.com/testing.html

# Gemfile
source "https://rubygems.org"
gem "sinatra"
gem "minitest"
gem "cuprite"
# main.rb
require "sinatra/base"
class MyApp < Sinatra::Base
  set :bind, "0.0.0.0"
  set :port, 8001
  get "/" do
    "hello"
  end
  run! if $0 == __FILE__
end
# minitest_cuprite.helper.rb 
require "capybara/cuprite"
Capybara.current_driver = Capybara.javascript_driver = :cuprite
Capybara.register_driver :cuprite do |app|
  Capybara::Cuprite::Driver.new app, "headless": false
end
require "capybara/minitest"
require "minitest/spec"
Minitest::Spec.include Capybara::DSL
Minitest::Spec.include Capybara::Minitest::Assertions
require "minitest/autorun"
# cat test.rb
require_relative "minitest_cuprite.helper"

ENV['APP_ENV'] = 'test'
require './main'
Capybara.server = :webrick
Capybara.app = MyApp.new

describe :test do
  before do
    Capybara.reset_sessions!
  end
  it do
    visit "http://localhost:8001"
    require "irb"
    binding.irb
  end
end

but when the browser window opens the localhost is down, it does not look like Sinatra was started. What do I miss here?

UPD: moving the require helper after the .app = is probably more correct but still no effect.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions