Acceptance testing of sinatra app using webrat fai

2019-08-16 21:32发布

问题:

I am trying to test a ruby authentication app using minitest and webrat but get errors.

Tests like visit '/' fail with an error Status 200 expected but was 404.

Tests containing code like fill_in :email, :with => "first@company.com" fail with error Could not find field: :email.

I read several sinatra, testing and webrat documents and forums. Some of them were old and suggested stuff like Sinatra::Default, but github.com/brynary/webrat/wiki/sinatra, Building a Sinatra App Driven By Webrat Tests and Learning From the Masters: Sinatra Internals are new, yet they still fail.

Basically, I didn't like sentence-like syntax of rspec, cucumber etc but do want to do behaviour driven development. I really like the minitest syntax, both tests and output and that is why I choose webrat for BDD. If I'm wrong about expecting webrat to fulfill acceptance testing requirements, please simply tell me that I should use this framework or that one.

Apart from that, the first parts of the main file and test file are below. I hope someone can explain me, what I am missing?

test_file

require "test/unit"
require "minitest/autorun"
require "rack/test"
require 'webrat'
require_relative "../lib/kimsin.rb"

Webrat.configure do |config|
  config.mode = :rack
end

ENV["RACK_ENV"] = "test"

class KimsinTests < Test::Unit::TestCase
  include Rack::Test::Methods
  include Webrat::Methods
  include Webrat::Matchers

  def app
    Sinatra::Application.new
  end

  def test_create_user
    visit "/user/new"
    fill_in :username, :with => "first@company.com"
    fill_in :password, :with => "abC123?*"
    fill_in :confirm_password, :with => "abC123?*"
    click_link "Register"
    assert 201, last_response.status, "Status 201 expected but was #{last_response.status}.\n#{error}"
    assert_contain /Logged in as first@company.com./, "No user created"
    assert_contain /Logout/, "Logout link not present"
  end

main_file

require "sinatra"
require "erb"
require_relative "../lib/kimsin/version"
require_relative "../lib/kimsin/user"

class Kimsin < Sinatra::Application
  use Rack::Session::Pool, :expire_after => 2592000
  set :session_secret, BCrypt::Engine.generate_salt

  configure :development do  
    DataMapper.auto_migrate!  
  end

  get "/" do
    if session[:user_id]
      user = User.get session[:user_id]
      email = user.email
      erb :index, :locals => { :email => email }
    else
      email = nil
      erb :index, :locals => { :email => email }
    end      
  end

回答1:

Using Sinatra with Webrat should work fine. I think that the errors that you are seeing are caused by the following method (around line 18 in your test file):

def app
  Sinatra::Application.new
end

This is setting up the Sinatra::Application base class to run your tests against when you really need to set up your own subclass Kimsin (because you are creating a modular style Sinatra app), i.e.

def app
  Kimsin.new
end

The 404 errors and missing fields are happening because Sinatra::Application doesn't define any of the routes you are testing.

You might also like to take a look at Capybara if you are looking for similar alternatives to Webrat.